2.6. Accessing Deleted Rows in a DataTable


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.


Use either a DataView or the Select( ) method of the DataTable to access deleted rows.

The sample code contains three event handlers:


Sets up the sample by creating a DataTable containing Orders data from Northwind. A view containing the Current rows is bound to a data grid on the form.

Current Rows RadioButton.CheckedChanged

Sets the view of the Orders data to display only Current rows. The text box displaying information about deleted rows is cleared.

Deleted Rows RadioButton.CheckedChanged

Sets the view of the Orders data to display only Deleted rows. The DataTable for the DataView is retrieved and the Select( ) method is used to get the Deleted rows. The overloaded indexer in C#, or Item( ) property in VB.NET, is used to retrieve the OrderID from the Original version of these deleted rows. This information is displayed in the text box on the form.

The C# code is shown in Example 2-6.

Example 2-6. File: AccessDeletedRowsForm.cs

// Namespaces, variables, and constants using System; using System.Configuration; using System.Text; using System.Data; using System.Data.SqlClient; private DataView dv; // . . . private void AccessDataSetDeletedRowsForm_Load(object sender, System.EventArgs e) { // Fill ...

Get ADO.NET Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.