How can I update the value of a DataGridViewCheckBoxCell immediately?

3.8k views Asked by At

I am using a DataGridViewCheckBoxCell in a DataGridView control. While the user clicks the checkbox, I verify certain conditions and set its value to false. However, the checkbox remains checked until the user moves to the next row. How can I fix this so the value is updated immediately

2

There are 2 answers

0
neminem On

Yep... not that you probably care at this point, but I recently ran into what I assume is the same issue: if your clicking the selected checkbox is the direct cause of it becoming checked or unchecked, then indeed, all you need to do is handle the DataGridView's CellDirtyStateChanged, and tell the grid to CommitEdit on itself. On the other hand, if a checkbox in a DataGridViewCheckBoxColumn changes state for any other reason than because you just clicked on it (for instance, the result of using a context menu to batch-toggle them, or you set it programmatically after verifying something), and that checkbox has focus, as far as I could tell nothing short of deselecting it would force it to redraw itself with the correct checkedness.

Sadly, I found no way around this, so I cheated: after the code responsible for programmatically toggling a cell's checkbox, if the grid's IsCurrentCellInEditMode was true, and its CurrentCell's column was the checkbox column, I just moved the CurrentCell a column over and then back. Not the prettiest solution, but I was sick of fighting with that control, and it worked without any obvious flicker or anything, so... good enough.

Of course, if you're just validating the one cell that was clicked, there are probably easier ways, involving validating the value itself, rather than handling validating in the click event and pushing the changed value yourself.

0
Even On

First Commit the changes and just change control to another cell and comeback.