When try to change the value of a DataGridVIew, the CurrentCellDirtyStateChanged event occurs and IsCurrentCellDirty property becomes true. At this time, when retrieve the cell value, it contains the value before the change.(However, appearance of the cell has changed.) Executing CancelEdit method cancels the changes, and executing CommitEdit method accepts the changes.
private void dgvSearch1_CurrentCellDirtyStateChanged(
object sender, EventArgs e) {
var dgv = (DataGridView)sender;
var current = dgv.CurrentCell;
if (dgv.IsCurrentCellDirty && current != null) {
if (current.ColumnIndex == colChecked1.Index) {
bool prevValue = (bool)current.Value;
if (prevValue) {
Messager.Error("This CheckBox already checked");
dgv.CancelEdit();
} else {
dgv.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
}
}