In VB.NET 2005 what is the best way to get control when someone checks or unchecks a checkbox cell in a datagridview.

I am getting conflicting results and it seems that either the current cell is the previous cell or the value is always false.

I just want a way that when someone checks or unchecks the cell I get control of the current cell and it has the checked value. Either the new value or the old as long as it is consistant.

Thanks

Dani AI

Generated

Short version: is correct that the checkbox edit is not committed until the edit is finalized, so reading the value in a click handler often yields the old/empty value. The common, reliable pattern is to force the edit to commit immediately (when the cell becomes dirty) and then react to the committed value in CellValueChanged. This avoids relying on CellClick (as suggested) which runs before the edit is committed.

Example (force commit as soon as the checkbox is toggled):

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

    If DataGridView1.IsCurrentCellDirty AndAlso _
       TypeOf DataGridView1.CurrentCell Is DataGridViewCheckBoxCell Then

        DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
    End If
End Sub

Then handle the change in CellValueChanged (read value safely and check indexes):

Private Sub DataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) _
    Handles DataGridView1.CellValueChanged

    If e.RowIndex < 0 Or e.ColumnIndex < 0 Then Return

    If DataGridView1.Columns(e.ColumnIndex).Name = "MyCheckColumn" Then
        Dim raw = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value
        Dim isChecked As Boolean = If(raw Is Nothing OrElse Convert.IsDBNull(raw), False, Convert.ToBoolean(raw))
        ' handle isChecked
    End If
End Sub

Notes and troubleshooting: calling CommitEdit from CellContentClick is an alternative if CurrentCellDirtyStateChanged feels unfamiliar. For bound grids verify DBNull handling and that the checkbox column is not in three-state mode.

Recommended Answers

All 3 Replies

I have tried CellClick but it doesn't work.

The checkbox cell
sender.item(e.ColumnINdex,e.Rowindes).value
or
DataDrridView1.item(e.ColumnINdex,e.Rowindex).value

is ALWAYS nothing. When or when it is not checked.

The idea is to do something when it is check just as if it was a normal checkbox.

Even when the I hard code the column (0 in my case).

Hello QBD, I understand your problem; unfortunately there is no easy solution. What you have to understand is like working directly in your database, any value changes you make to a cell dont truly take effect in that cell/record until you move to another record/row; at which time the new value is then validated and changes commited.

So even though the CellClick event may actuall fire each time its clicked (unlike most of the other events) its still not seeing that new value until you move to the next row.

The best that you can manage is checking that value when it does move to the next row in a consistent manner. The below event & sample triggers when you move to the next row, but will give you the changed value from the row you just left.

Private Sub dgv_CellValueChanged(...) Handles dgv.CellValueChanged

    Dim sbMsg As New StringBuilder

     With sbMsg
        .AppendLine(String.Format("Row Index: {0}", e.RowIndex))
        .AppendLine(String.Format("Column Index: {0}", e.ColumnIndex))
        .AppendLine(String.Format("Column Name: {0}", dgv.Columns(e.ColumnIndex).Name))
        .AppendFormat("Value: {0}", dgv.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)
    End With
    MessageBox.Show(sbMsg.ToString)

End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.