Error determining flag state in DataGridView

I have a DataGridView control in a .Net application that contains a checkbox column. I would like the user to be able to edit these checkboxes. The problem I am facing is that I cannot determine the state of the checkbox after the user checks.

If the check box was originally set, it will be checked as soon as the DataGridViewCheckBoxCell receives focus. But, if I click on the checkbox again and cancel it, it will return anyway. From now on, it will always be checked regardless of the actual state of the flag until it loses focus and gets it again.

Similarly, if the check box was initially unchecked, then when it is focused, it will not be marked in the click event, regardless of what the check box actually has.

Here is my code.

    Private Sub grdTemplates_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdTemplates.CellContentClick
    Dim strValue As String = ""
    Try
        If Me.grdTemplates.Columns(e.ColumnIndex).Name = "colCurrentTemplate" Then
            'The user clicked on the checkbox column
            strValue = Me.grdTemplates.Item(e.ColumnIndex, e.RowIndex).Value

            'THIS VALUE NEVER CHANGES WHILE THE DataGridViewCheckBoxCell HAS FOCUS
            Me.lblTemplates.Text = strValue
        End If

    Catch ex As Exception
        HandleError(ex.ToString)
    End Try

End Sub

Thanks in advance,

Mike

+5
source share
1 answer

Include this in your code:

Sub dataGridView1_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As EventArgs) Handles dataGridView1.CurrentCellDirtyStateChanged
    If dataGridView1.IsCurrentCellDirty Then
        dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
    End If
End Sub

Source: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcelldirtystatechanged.aspx

+4
source

All Articles