DataGridView - update checkbox on click

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,827
Location
Norway
Programming Experience
10+
I have a DataGridView in VS2005 where one column is of type DataGridViewCheckBoxColumn. User can review this grid, check some rows with the checkboxes and use (context)menu to generate a list of selected rows.
Problem is that the last checkbox clicked is not commited until user clicks another row. Row appear checked, but is missing in the generated list.
This is the code trying to fix it, but not working, I'm trying to commit the "edit-in-progress" immediately when user click checkbox:
VB.NET:
Private Sub DGW_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGW.CellClick
  If e.ColumnIndex = 0 Then
    DGW.CommitEdit(DataGridViewDataErrorContexts.Commit)
  End If
End Sub
Anyone got a simple solution?
 
Moved to more appropriate forum.

I'm sure I've read a solution to this problem elsewhere but I can't remember where or exactly what it was. I've looked through the DataGridView members to see if anything rang a bell but nothing did. Have you tried using EndEdit instead of CommitEdit?
 
EndEdit will cancel that pending checkbox edit...
 
Yes, I did read that, but when doing it in CellClick event, it effectively cancelled the operation.
Doing EndEdit in MouseUp event did the trick. (Also CommitEdit worked for MouseUp event.)
I forgot the basic down-click-up mouse event order..
One reason for me heading into the click event, is it also handles checkbox selection by keyboard space key (but it's rarely used anyway).
Thanks for your input!
 
Back
Top