Reputation: 1952
I have a datagridview1
. When user edit the cell value, I want to check the validation of data. If not valid then rollback the cell value to old value.
In the CellValidating
event, I used:
e.Cancel = true;
but the value not rolled back How can I do that?
Upvotes: 4
Views: 5408
Reputation: 5454
Setting e.Cancel = true
simply cancels the edit from exiting, forcing the user to edit the current cell until it is valid. Instead, allow the edit to occur, but reset the value to the original. Replace the line of code you've posted with the following:
dataGridView1.EditingControl.Text = dataGridView1.CurrentCell.Value.ToString();
Edit: Or just replace the above with the following line of code. This has further advantage of not losing the data type and cancels the addition of the NewRow
if one was added.
this.dataGridView1.CancelEdit();
Upvotes: 8