views:

129

answers:

1

Hi,

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

A: 

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

Kyle Rozendo
dt.RejectChanges() is giving me NullReferenceExcpetion althu the DataSource is not null
Mustafa A. Jabbar
It means it cannot convert the DataSource into a DataTable.
Kyle Rozendo