none
Accessing DataRow Column Data Post Delete RRS feed

  • Question

  • Hello, My form displays a DataGrid that the user has the option of removing items from using the DataRow.Delete(). When the user is happy and presses “Save” I iterate throw all the rows in the DataTable and update each row. If the update is successful (row is deleted from the database) I’d like to call another query based on the recently removed row. However I cannot access the column data of this row as it throws an exception. What is the best method of handling this scenario? Thank you, Brian
    Thursday, November 11, 2010 7:19 AM

Answers

  • Before you send the delete back to the database (before the DataTable.AcceptChanges() gets called, either explicitly by you, or via the Adapter.Update() method), create a new DataTable containing only the Deleted Rows like this:

    dtDeleted = MyDataTable.GetChanges(DataRowState.Deleted);
    dtDeleted.RejectChanges(); // so they're not marked as deleted
    

    Then you can go ahead and update your database and DataTable and still be able access all the Rows in the dtDeleted table as you normally would.


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com

    • Marked as answer by Brian Faivre Tuesday, November 16, 2010 12:28 AM
    Sunday, November 14, 2010 1:50 AM

All replies

  • If you are getting DeletedRowInaccessibleException then I believe you can access deleted values like this: row["SomeColumn", DataRowVersion.Original]
    MCTS, CodeProject MVP 2008
    Thursday, November 11, 2010 8:00 AM
  • I don't think you can use the DataRowVersion approach after you have updated the DataTable.
    It will then return the value of the row that is now on that position. For example:

    Table.Row[3].Delete()
    Table.Update
    Table.Row[3].RowVersion will now return what was previously on index 4.

    Hope this makes sense.

    Maybe you can use the Table.RowDeleting event.


    This posting is provided "AS IS" with no warranties.
    Thursday, November 11, 2010 3:45 PM
  • Before you send the delete back to the database (before the DataTable.AcceptChanges() gets called, either explicitly by you, or via the Adapter.Update() method), create a new DataTable containing only the Deleted Rows like this:

    dtDeleted = MyDataTable.GetChanges(DataRowState.Deleted);
    dtDeleted.RejectChanges(); // so they're not marked as deleted
    

    Then you can go ahead and update your database and DataTable and still be able access all the Rows in the dtDeleted table as you normally would.


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com

    • Marked as answer by Brian Faivre Tuesday, November 16, 2010 12:28 AM
    Sunday, November 14, 2010 1:50 AM