none
Restore deleted row in DataSet RRS feed

  • Question

  • Hi.

    I've got a problem restoring deleted row in a dataset. According to this http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/ea284c99-84eb-4a11-a34c-4e230808a116 RejectChanges() should restore my rowstate (deleted -> unchanged). But this isn't working for me.

    My code look somehting like that:

     

    foreach
     (MyDataRow row in
     MyDataSet.MyDataTable.GetChanges(DataRowState.Deleted))
        {
         //change row back to unchanged, need to access data
    
         row.RejectChanges();
    
         //check some things on the deleted row (e.g. contraints match...)
    
         if
     (row.SomeFKID == someFKID)
          continue
    ;
         //delte row again if it was no match for me
    
         row.Delete();
        }
    

     

     

    After this code is passed the row is still marked as deleted (even if its state was "unchanged" in the loop).

    MyDataSet is the datasource for a ListBox. The user can add/remove items. A "discard" button for this ListBox should discard all changes made since the last AcceptChanges call. Added items should be deleted (works fine with row.RejectChanges()) and removed items should be restored. I can't call RejectChanges() on the DataTable because there are other lists using the same DataSet as datasource (other FK) and I just want to discard the changes for the ListBox where the button belongs to.

     

    Thx for any suggestions.

    Monday, November 15, 2010 1:17 PM

Answers

  • The problem is that the DataTable.GetChanges() returns a new DataTable, consequently the Rows in that DataTable are NOT the same Rows that were in your original DataTable. Try something like this instead:

    foreach (MyDataRow row in MyDataSet.MyDataTable)
    {
      if (row.RowState == DataRowState.Deleted)
      {
        //change row back to unchanged, need to access data
        row.RejectChanges();
    
        //check some things on the deleted row (e.g. contraints match...)
        if (row.SomeFKID == someFKID)
          continue;
         
        //delte row again if it was no match for me
        row.Delete();
      }
    }
    

    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Proposed as answer by Mike_999 Tuesday, November 16, 2010 6:17 AM
    • Marked as answer by WhiteBahamut Tuesday, November 16, 2010 8:47 AM
    Tuesday, November 16, 2010 6:09 AM

All replies

  • The problem is that the DataTable.GetChanges() returns a new DataTable, consequently the Rows in that DataTable are NOT the same Rows that were in your original DataTable. Try something like this instead:

    foreach (MyDataRow row in MyDataSet.MyDataTable)
    {
      if (row.RowState == DataRowState.Deleted)
      {
        //change row back to unchanged, need to access data
        row.RejectChanges();
    
        //check some things on the deleted row (e.g. contraints match...)
        if (row.SomeFKID == someFKID)
          continue;
         
        //delte row again if it was no match for me
        row.Delete();
      }
    }
    

    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Proposed as answer by Mike_999 Tuesday, November 16, 2010 6:17 AM
    • Marked as answer by WhiteBahamut Tuesday, November 16, 2010 8:47 AM
    Tuesday, November 16, 2010 6:09 AM
  • Thanks. That was what I needed.
    Tuesday, November 16, 2010 8:46 AM
  • You're welcome ... glad I could help! =0)
    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Tuesday, November 16, 2010 3:11 PM
  • This worked for me:

    DataView dv = new DataView(MyDataSet.MyDataTable, "SomeFKID=" + someFKID, string.Empty, DataViewRowState.Deleted);
     if (dv.Count == 1) // We found the row, and it was DataViewRowState.Deleted
      dv[0].Row.RejectChanges(); // "UnDelete" it

    Wednesday, January 26, 2011 11:38 AM
  • That looks good too ...


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Wednesday, January 26, 2011 4:38 PM