4.7. Accessing Deleted Rows in a DataTable
Problem
When you delete rows from a DataSet they are really marked for deletion until changes are committed by calling AcceptChanges() either directly or indirectly. You want to access the rows that you have deleted from a DataTable.
Solution
Use one of the four techniques demonstrated in this solution.
The solution fills a DataSet with the TOP 3 records from the Person.Contact table in the AdventureWorks database. The second row is deleted. The deleted row is accessed and written to the console using the following techniques:
Iterating over the records in the
DataTable, identifying the deleted rows as having theirRowStateproperty being set toDataRowState.Deleted, and accessing the original values for the deleted row using the field accessor of theDataRowthat takes a second argument and specifying it asDataRowVersion.Original.Creating a
DataViewof only deleted records in the table using an overload of theDataViewconstructor with the row state argument set toDataViewRowState.Deleted.Creating a
DataViewbased on the table and filtering only deleted records by setting theRowStateFilterproperty of theDataViewtoDataViewRowState.Deleted.Using the
Select()method with the row state set toDataViewRowState.Deletedto get aDataRowarray of deleted records from theDataTableand accessing the values for the deleted rows through their original values by specifying a row state ofDataRowVersion.Originalin theDataRowfield accessor.
The ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access