And if all checkBoxes will be unchecked, the button has to be Disabled (or Invisible) again?
You can create some class boolean flag, which will be set to true, as soon as you check at least one checkBox (or even better would be to create a counter, so you wont need to check all the rows if there are checkBoxes checked or unchecked. Simply, when you check one, counter rises by 1, and when you remove tick on checkBox, you subtract by 1. So when the counter is zero (0), you disable button, and when is higher then zero ( > 0 ), you enable it.
But this you cannot do on the cell click. You will have to use two other events:
1. CurrentCellDirtyStateChanged
2. CellValueChanged
to get the state of checkbox.
Here is the example code:
Private counter As Integer
Public Sub New()
InitializeComponent()
End Sub
Private Sub dataGridView1_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs)
If dataGridView1.IsCurrentCellDirty Then
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
End If
End Sub
Private Sub dataGridView1_CellValueChanged(obj As Object, e As DataGridViewCellEventArgs)
If e.ColumnIndex = 1 Then
'compare to checkBox column index
Dim cbx As DataGridViewCheckBoxCell = DirectCast(dataGridView1(e.ColumnIndex, e.RowIndex), DataGridViewCheckBoxCell)
If Not DBNull.Value.Equals(cbx.Value) AndAlso CBool(cbx.Value) = True Then
'checkBox is checked - do the code in here!
counter += 1
Else
'if checkBox is NOT checked (unchecked)
counter -= 1
End If
End If
If counter > 0 Then
btnEdit.Enabled = True
ElseIf counter = 0 Then
btnEdit.Enable = False
End If
End …