取消选中数据 DataGridView 中的复选框时清除文本框

问题描述 投票:0回答:1

在 FormLoad 中的 DataGridView2 中使用此复选框

  Dim chk1 As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn()
  DataGridView2.Columns.Add(chk1)
  chk1.HeaderText = "Keuze"
  chk1.Name = "chk"

当选择一个复选框填充一些文本框时

 Private Sub DataGridView2_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView2.CellContentClick
     If e.ColumnIndex = 0 AndAlso e.RowIndex >= 0 Then
         For Each row As DataGridViewRow In DataGridView2.Rows
             row.Cells(e.ColumnIndex).Value = False
         Next
     End If
     Dim senderGrid As DataGridView = sender
     Dim data = senderGrid.Rows(e.RowIndex).DataBoundItem

     If senderGrid.Columns(e.ColumnIndex).GetType() Is GetType(DataGridViewCheckBoxColumn) And e.RowIndex >= 0 Then
         TBterug.Text = DataGridView2.CurrentRow.Cells("Todo").Value.ToString()
         CBbelangrijkterug.Text = DataGridView2.CurrentRow.Cells("Belangrijk").Value.ToString()
         Id.Text = DataGridView2.CurrentRow.Cells("Id").Value.ToString()
         LBdelete.Visible = True
         LBuncheck.Visible = True
     End If

 End Sub

但是当我取消选中复选框时我想清空文本框,我需要添加什么才能完成这项工作?

我尝试了这个,但没有运气选中/取消选中datagridview上的复选框

vb.net checkbox datagridview
1个回答
0
投票

如果您只想允许一项选择,请不要等待提交另一项选择,并尽早采取行动来阻止它。在

DataGridView
控件中,如果 CurrentCell 属于目标 DataGridView.CancelEdit
 并且当前未检查,但它是 
dirty
并且当前未检查,则可以通过处理 CurrentCellDirtyStateChanged 来调用
DataGridViewCheckBoxColumn 方法来采取此类操作有未提交的更改。

例如:

Private Sub DataGridView2_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DataGridView2.CurrentCellDirtyStateChanged
    Dim dgv = DirectCast(sender, DataGridView)

    If TypeOf dgv.CurrentCell Is DataGridViewCheckBoxCell AndAlso
        dgv.IsCurrentCellDirty AndAlso
        Not CBool(dgv.CurrentCell.FormattedValue) Then

        Dim col = dgv.CurrentCell.ColumnIndex

        If dgv.Rows.Cast(Of DataGridViewRow).
            Any(Function(row) CBool(row.Cells(col).FormattedValue)) Then
            dgv.CancelEdit()
        End If
    End If
End Sub

现在,要在提交更改之前重置显示和命令控件,请处理 DataGridView.CellContentClick 事件,以在 DataGridViewCell.EditedFormattedValue 属性为 true 时清除并填充/显示/启用控件,或者只是清除/禁用/如果 DataGridViewCell.FormattedValue 属性为 true,则隐藏它们。

Private Sub DataGridView2_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView2.CellContentClick
    If e.ColumnIndex = 0 AndAlso e.RowIndex >= 0 Then
        Dim dgv = DirectCast(sender, DataGridView)
        If CBool(dgv(e.ColumnIndex, e.RowIndex).EditedFormattedValue) Then
            ResetControls(dgv.Rows(e.RowIndex))
        ElseIf CBool(dgv(e.ColumnIndex, e.RowIndex).FormattedValue) Then
            ResetControls()
        End If
    End If
End Sub

Private Sub ResetControls(Optional row As DataGridViewRow = Nothing)
    If row IsNot Nothing Then
        TBterug.Text = row.Cells("Todo").Value?.ToString()
        CBbelangrijkterug.Text = row.Cells("Belangrijk").Value?.ToString()
        Id.Text = row.Cells("Id").Value?.ToString()
        LBdelete.Visible = True
        LBuncheck.Visible = True
    Else
        TBterug.Text = ""
        Id.Text = ""
        CBbelangrijkterug.Text = ""
        LBdelete.Visible = False
        LBuncheck.Visible = False
    End If
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.