CellValidating, . ( ), .
1) You can cancel the event. The user receives an error icon in the row and cannot leave the cell. They are locked in cell editing mode until they lock the cell (Enter, Tab) with valid data.
2) You can drop the value to another value (previous value, some default value).
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
DataGridView grid = sender as DataGridView;
if (grid.Columns[e.ColumnIndex].HeaderText == "Text ID")
{
string value = (string)e.FormattedValue;
if (value.Contains(" "))
{
grid.Rows[e.RowIndex].ErrorText = "String IDs cannot contain spaces.";
e.Cancel = true;
}
}
else if (grid.Columns[e.ColumnIndex].HeaderText == "Platform")
{
if (grid.EditingControl != null && (string)e.FormattedValue != "All")
{
string oldvalue = (string)grid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
grid.EditingControl.Text = "All";
}
}
}
source
share