locked
How to avoid DataView changes to be thrown to DataTable RRS feed

  • Question

  • Hi, 

       I have the following scenario:

       I have defined a bindingSource, whose dataSource property has been initialized to be the dataView of a dataTable. To be more specific this dataview is used in the grid of a pop up. The effect i´m trying to get is for the dataTable to get the changes made on the dataView (and thus, on the grid) only after i call the endEdit on the bindingSource, thing that will happen when the user clicks the Accept botton of the pop up.  Otherwise, in case the user regrets about the changes he/she clicks the Cancel button, which in this case will call the CancelEdit of the binding source (and thus, the changes made on the grid won´t be reflected into the DataTable).

       It seems i´m missing something here because when i call cancelEdit, changes are still being reflected in the DataTable. Can it be the case that, for some reason, EndEdit has been called implicitly? Any hints?

     

    Thanks in advance, 

    Paola 

     

    Wednesday, August 11, 2010 5:13 PM

Answers

  • No, DataView does not have this capability -- it writes through to the underlying DataTable.  However, for modal actions like this, I believe there is a reasonable workaround.

    A useful technique can be to use the Copy method to duplicate the original DataTable (or entire DataSet) and then bind to the copy.  To commit the changes, use DataSet or DataTable Merge method to re-sync into the main DataSet.  This sounds like overkill, but since this is part of a user interface, if it is too slow, you are probably working with too much data anyways.

    Note that when Copy is called on a strongly-typed DataSet, the strong-typing is preserved but you need to do a cast on the result of Copy back to the type of your strongly-typed DataSet.

    Note that Merge will not automatically take care of rows that have been deleted in the copy (i.e., row still exists in original but no longer in the copy).  I have had to write code to loop through the rows in the original and look for what rows need Delete called on them by not have a corresponding row in the copy.

    I have used techniques like this very successfully in the past.

    • Marked as answer by Paola M Friday, August 13, 2010 1:37 PM
    Thursday, August 12, 2010 12:12 AM