Access Deleted Row Columns / Information in DataTable - ADO.NET Tips
by David Hayden ( .NET Developer )
This is one of those ADO.NET 2.0 questions that I just answered in the MSDN Forums, and I thought I would share the information.
Sometimes you may need to access the information in a deleted row in a DataTable. If you access the deleted row direcly, you will get an exception saying that the row has been deleted.
To avoid the exception, but still access the information in the deleted row of the DataTable, you just need to specify that you want to get the information from the original version of the row by specifying DataRowVersion.Original as follows:
if (dataRow.RowState == DataRowState.Deleted)
id = (string)dataRow["CustomerID", DataRowVersion.Original];
You can get only the deleted rows from a DataTable by iterating through all the rows and checking the DataRowState, or you could simply create a DataView that filters on DataViewRowState.Deleted:
// Get Only Deleted Rows in DataTable
DataView dv = new DataView(sourceDataTable,
null, null, DataViewRowState.Deleted);
You can also convert that DataView to a DataTable using the new DataView.ToTable() method:
DataTable dt = dv.ToTable();
The rows in the new DataTable, dt, will not be marked as DataRowState.Deleted but rather DataRowState.Added, because this is a new DataTable. You can easily iterate through this table now as none of the rows will be mark deleted and you will have full access to the information without specifying DataRowVersion.
Source: David Hayden ( .NET Developer )
ADO.NET Tips and Tricks