I have a DataGridView with a text column and a checkbox column. When the user clicks the checkbox I want to prompt the user. I've got this working using the code below:
private void grid_CurrentCellDirtyStateChanged( object sender, EventArgs e )
{
var grid = sender as DataGridView;
if ( grid.IsCurrentCellDirty)
grid.CommitEdit( DataGridViewDataErrorContexts.Commit );
}
However, when I try to type in the text column it keeps committing as I type instead of when I'm finished typing. This causes the text cell to keep highlighting itself and only allowing me to enter a single character. How do I modify this event to only handle when the checkbox value changes?
What you need to do is only pay attention to the checkbox, not the text. The solution I have found for a similar problem was to put an event on CellEnter that recorded what field was being edited. The dirty handling event then checks this to decide what to do.
Why the coordinates aren't available in the dirty check I can't imagine.
I just had the same problem. If you want to pay attention to specific column (CheckBox Column) this will work for you.
private void grid_CurrentCellDirtyStateChanged( object sender, EventArgs e )
{
var grid = sender as DataGridView;
if ( grid.IsCurrentCellDirty && grid.CurrentCell.ColumnIndex == 1 )
grid.CommitEdit( DataGridViewDataErrorContexts.Commit );
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With