1

I have a DataTable and a Grid (actually a DevExpress Winforms XtraGrid). I create a DataView on the table that is a subset of its rows. This DataView is the DataSource of the grid.

The user types in a new row into the grid and posts the change (clicks the checkmark for the grid). Then they decide they want to revert all their changes by clicking the cancel button (on my form).

In the button handler I want to revert the rows in the DataView to their original state. I do not want to revert all the rows in the DataTable.

I look through the rows of the DataView, rejecting changes on each row, like:

    foreach (DataRowView drv in myDV)
    {
        MyTableRow row = (MyTableRow)drv.Row;
        row.RejectChanges();
    }

This works fine for reverting updates. However, if the user had inserted a new row in the grid I get the exception: System.Data.RowNotInTableException thrown when RejectChanges is called. Here is the stack trace to the exception:

   at System.Data.DataRow.GetDefaultRecord()
   at System.Data.DataColumn.CheckNullable(DataRow row)
   at System.Data.DataTable.RaiseRowChanging(DataRowChangeEventArgs args, DataRow eRow, DataRowAction eAction, Boolean fireEvent)
   at System.Data.DataTable.SetNewRecordWorker(DataRow row, Int32 proposedRecord, DataRowAction action, Boolean isInMerge, Boolean suppressEnsurePropertyChanged, Int32 position, Boolean fireEvent, Exception& deferredException)
   at System.Data.DataRow.RejectChanges()

This is not the behavior I expected. I expected that it would simply remove this row from the datatable and everything would be cool.

Any thoughts on how to get the behavior I am looking for?

shindigo
  • 1,267
  • 15
  • 29

1 Answers1

0

Turns out this is a peculiarity of the DevExpress Grid.

I misstated when I said that the user clicked the checkmark to post their changes. That case works fine. The problem occurs when they DO NOT click the checkmark to save the new row and click Cancel instead.

The solution is to call:

myGridView.CancelUpdateCurrentRow();

before looping through the DataView's rows.

shindigo
  • 1,267
  • 15
  • 29