I have a DataGridView that is bound to a list of object. It has some columns that the user can edit. There are certain inputs that are not allowed for a row as a whole. How can I roll back if the user enters invalid inputs in some cell. I tried using the RowValidating event handler but it was not called after cell value has been changed. Even when I implemet CellValueChanged, I still cannot roll back the changes. ... Any idea how to accomplish this
When databinding exists, for me it works with:
myBindingSource.CancelEdit();
myDataGridView.RefreshEdit();
Once editing has been completed and you validate the changes, you can do this:
DataTable dt = this.dataGridView.DataSource as DataTable;
dt.RejectChanges();
From MSDN:
When the DataTable.RejectChanges method is called, any rows still in edit-mode cancel their edits. New rows are removed. Modified and deleted rows return back to their original state (DataRowState.Unchanged).
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