I have a DataGridView
with two checkbox columns. When any rows on both of them are checked, I want to clear the checkmarks. The grid is populated from a DataTable. It works like this:
if (bothColumnsAreChecked)
{
DataRow first = SelectedFirstItems.First();
DataRow second = SelectedSecondItems.First();
// stuff...
first["IsCheckedFirst"] = false;
second["IsCheckedSecond"] = false;
}
As per this, I have modified the code to fire the CellValueChanged
event immediately after clicking the checkbox, not when losing focus:
void brandsGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
brandsGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
The problem is that, after clearing the checkmarks, one of the rows is still selected (because I just clicked on a checkbox in that row), and the checkmark on this row is still visible -- it doesn't disappear until I unselect the row. How do I achieve that? I tried:
1) Refresh()
on the grid. No avail; the grid is refreshed, but not redrawn before moving the cursor.
2) CommitEdit()
and EndEdit()
. This doesn't even clear the last selected checkbox.
What can I do?
EDIT: I'm bad at explaining. Here's a simple walkthrough:
There are two checkbox columns: c1 and c2.
1) I click c1 on row r1. c1 becomes checked and r1 becomes selected.
2) I click c2 on row r2 (or even r1). c2 becomes checked and r2 becomes selected.
3) c1 and c2 are now cleared (after logging checkbox info somewhere).
The problem is that, while the chackmark disappears from c1 in r1, it stays there, in the checkbox on r2, until r2 loses focus.
Hope that helps.