Argh! This is why I avoid DataGridViews like the plague. I was working today in a Win Forms app attempting to perform a function if all checkboxes in my DataGridView column had been set to “true” (aka. the user has them all selected).
After much anguish and what I’m sure was a bug with the DataGridView, turns out to be by design. This is typical because I should have known there is always some hazy way of performing something with this control because it’s so freakin’ huge! Embarrassingly, when I see the property value I should have accessed it probably makes sense *grumble*.
Anyway, on to the goods, this is mainly a post for my Google searchers out there who also have had the same problem, if you are attempting to read the checkbox value from within a DataGridView Cell, specifically a DataGridViewCheckBoxCell and find that the Value property is constantly returning True even though you have unchecked it, you are accessing the wrong property.
The property you in fact need is EditedFormattedValue instead, as this will return the current (formatted) value of the cell regardless of if the cell has been committed (finished editing) or not.
Check the below sample on how this works by disabling a button if no checkboxes have been selected within a DataGridView:
private void dataGridViewFiles_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
// Ignore clicks that are not on checkbox cells.
if (e.ColumnIndex == ((DataGridView)sender).Columns[0].Index)
{
bool selected = false;
foreach (DataGridViewRow row in ((DataGridView)sender).Rows)
selected = Convert.ToBoolean(row.Cells[0].EditedFormattedValue)
? Convert.ToBoolean(row.Cells[0].EditedFormattedValue)
: selected;
// Set button state based on if any checkboxes are selected.
this.ButtonNext.Enabled = selected;
}
}
private void dataGridViewFiles_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
// Ignore clicks that are not on checkbox cells.
if (e.ColumnIndex == ((DataGridView)sender).Columns[0].Index)
{
bool selected = false;
foreach (DataGridViewRow row in ((DataGridView)sender).Rows)
selected = Convert.ToBoolean(row.Cells[0].EditedFormattedValue)
? Convert.ToBoolean(row.Cells[0].EditedFormattedValue)
: selected;
// Set button state based on if any checkboxes are selected.
this.ButtonNext.Enabled = selected;
}
}
Note! It is important you subscribe to the “CellContentDoubleClick” event as well to capture those checkbox clicks that occur on a double click! Otherwise the user can get the button in to a state where it is enabled without any checkboxes selected!
4 Comments
Many thanks for this post! It was the only one on the first pages on google to help me sove this problem.
THANK YOU THANK YOU THANK YOU. Unfortunately, I did get a couple more gray hairs as a result of not stumbling upon your post quite quickly enough.
You, sir, are my Hero of the Day.
Nobody ever talks about this damn property. THANKS!
Thank you!
I’d like to add: since the user can check/uncheck using the keyboard (space bar) and move around also using the keyboard (arrows, page up, page down), consider handling the CellLeave event instead of click events.
JM2B
Post a Comment