I need help on DataGridView CheckBox. How can make a button enable or visible while only one checkBox of a datagrideview is checked?
I tryed this , and it works but it only works when the datagrideview checkbox column is selected but i need when only one checkbox is checked then the button will enable or visible, please help me.

Private Sub dgvEmployee_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvEmployee.CellClick
        If dgvEmployee.Item(clmCheck.Index, e.RowIndex).Selected = True Then
                btnEdit.Enabled = True
            Else
                btnEdit.Enabled = False
            End If
    End Sub

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 Sub

Edited 5 Years Ago by Mitja Bonca: n/a

I need help on DataGridView CheckBox. How can make a button enable or visible while only one checkBox of a datagrideview is checked?
I tryed this , and it works but it only works when the datagrideview checkbox column is selected but i need when only one checkbox is checked then the button will enable or visible, please help me.

VB.NET Syntax (Toggle Plain Text)

Private Sub dgvEmployee_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvEmployee.CellClick
If dgvEmployee.Item(clmCheck.Index, e.RowIndex).Selected = True Then
btnEdit.Enabled = True
Else
btnEdit.Enabled = False
End If
End Sub

dim counter as int 

For Each row As DataGridViewRow In DataGridView1.Rows 
if row.cells("Column Name").value = "True" 
then 
counter += 1 
End if

Next 

if counter = 1 then 
btnEdit.Enabled = True
Else
btnEdit.Enabled = False
End If

Edited 5 Years Ago by adam_k: n/a

This article has been dead for over six months. Start a new discussion instead.