Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to roll back changes in gridview in case of incorrect input

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

like image 224
mustafabar Avatar asked Mar 17 '10 06:03

mustafabar


2 Answers

When databinding exists, for me it works with:

myBindingSource.CancelEdit();
myDataGridView.RefreshEdit();
like image 68
mihai71 Avatar answered Nov 05 '22 07:11

mihai71


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).

like image 39
Kyle Rosendo Avatar answered Nov 05 '22 08:11

Kyle Rosendo