DataGridView Checkbox Column Handling in VB.NET

Posted by VIJI Tuesday, June 30, 2009

Problem Description

We encounter a situation where we need to edit the Datagridview rows based on the value of Datagridview Checkbox column Cell.

But the datagridview Checkbox cell will not commit immediately after checking the cell.It will be committed only after checking the next cell.

So the below solution makes the cell to commits it's value once it is checked.

Solution Description

Bind the data to the Datagridview.Place the below code in the CurrentCellDirtyStateChanged event of the datagridview.

When the user checks the cell,CurrentCellDirtyStateChanged event wiil be fired and then cell's dirty stae is checked.

If so, CommitEdit method is called.


Sample Coding


Private Sub DataGridView1_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged

Dim dgCheckBoxCell As DataGridViewCheckBoxCell

Try
'Checking whether the Datagridview Checkbox column is the first column
If DataGridView1.CurrentCellAddress.X = 0 Then
dgCheckBoxCell = DataGridView1.CurrentRow.Cells(0)

If (DataGridView1.IsCurrentCellDirty) Then 'Checking for dirty cell
DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit) 'If it is dirty, making them to commit

If (dgCheckBoxCell.Value) Then
'Enabling the selected row editable for sample.

DataGridView1.CurrentRow.Cells("EmpID").ReadOnly = False
DataGridView1.CurrentRow.Cells("Designtn").ReadOnly = False
DataGridView1.CurrentRow.Cells("Location").ReadOnly = False
DataGridView1.CurrentRow.Cells("Experience").ReadOnly = False

Else
'Disabling the selected row for editing.

DataGridView1.CurrentRow.Cells("EmpID").ReadOnly = True
DataGridView1.CurrentRow.Cells("Designtn").ReadOnly = True
DataGridView1.CurrentRow.Cells("Location").ReadOnly = True
DataGridView1.CurrentRow.Cells("Experience").ReadOnly = True

End If
End If
End If

Catch ex As Exception


End Try

End Sub

1 Responses to DataGridView Checkbox Column Handling in VB.NET

  1. anu Says:
  2. Viji good solution

     

Post a Comment