When a cell has been selected and edited, the DataGridView
property IsCurrentCellDirty
is set to True
. If you catch the event handler when this state changes on a DataGridViewCheckBoxCell
, you can call DataGridView.EndEdit()
to finalize those changes immediately.
this.dataGridView1.CurrentCellDirtyStateChanged += DataGridView1_CurrentCellDirtyStateChanged;
private void DataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (this.dataGridView1.IsCurrentCellDirty && this.dataGridView1.CurrentCell is DataGridViewCheckBoxCell)
{
this.dataGridView1.EndEdit();
}
}
Further explanation:
Behind the scenes, DataGridView.IsCurrentCellDirty
is updated whenever you edit the current cell. The first line of code above allows you to attach to the CurrentCellDirtyStateChanged
event your own event handler (DataGridView1_CurrentCellDirtyStateChanged
) . So whenever the cell becomes dirty, behind the scenes will call the base level event and then your method as well. Without that line, your method will not be called. The +=
operator is what attaches your method to the event's call-chain.
For example, adding the following handlers:
this.dataGridView1.CurrentCellDirtyStateChanged += DataGridView1_Example1;
// this.dataGridView1.CurrentCellDirtyStateChanged += DataGridView1_Example2;
this.dataGridView1.CurrentCellDirtyStateChanged += DataGridView1_Example3;
private void DataGridView1_Example1(object sender, EventArgs e)
{
Console.WriteLine("Example 1");
}
private void DataGridView1_Example2(object sender, EventArgs e)
{
Console.WriteLine("Example 2");
}
private void DataGridView1_Example3(object sender, EventArgs e)
{
Console.WriteLine("Example 3");
}
When the dirty state changes, you'll see the following output. Notice the 2nd event handler was excluded:
// Example 1
// Example 3