How to detect DataGridView CheckBox event change?

后端 未结 16 2460
离开以前
离开以前 2020-11-27 12:43

I have a winforms app and want to trigger some code when a checkbox embedded in a DataGridView control is checked / unchecked. Every event I have tried either

16条回答
  •  伪装坚强ぢ
    2020-11-27 13:29

    I have found a simpler answer to this problem. I simply use reverse logic. The code is in VB but it is not much different than C#.

     Private Sub DataGridView1_CellContentClick(sender As Object, e As 
     DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    
        Dim _ColumnIndex As Integer = e.ColumnIndex
        Dim _RowIndex As Integer = e.RowIndex
    
        'Uses reverse logic for current cell because checkbox checked occures 
         'after click
        'If you know current state is False then logic dictates that a click 
         'event will set it true
        'With these 2 check boxes only one can be true while both can be off
    
        If DataGridView1.Rows(_RowIndex).Cells("Column2").Value = False And 
           DataGridView1.Rows(_RowIndex).Cells("Column3").Value = True Then
            DataGridView1.Rows(_RowIndex).Cells("Column3").Value = False
        End If
    
        If DataGridView1.Rows(_RowIndex).Cells("Column3").Value = False And 
        DataGridView1.Rows(_RowIndex).Cells("Column2").Value = True Then
            DataGridView1.Rows(_RowIndex).Cells("Column2").Value = False
        End If
    
    
    End Sub
    

    One of the best things about this is no need for multiple events.

提交回复
热议问题