2.6. 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 either a DataView or the Select( ) method of the DataTable to access
deleted rows.
The sample code contains three event handlers:
Form.LoadSets up the sample by creating a
DataTablecontaining Orders data from Northwind. A view containing theCurrentrows is bound to a data grid on the form.- Current Rows
RadioButton.CheckedChanged Sets the view of the Orders data to display only
Currentrows. The text box displaying information about deleted rows is cleared.- Deleted Rows
RadioButton.CheckedChanged Sets the view of the Orders data to display only
Deletedrows. TheDataTablefor theDataViewis retrieved and theSelect( )method is used to get theDeletedrows. The overloaded indexer in C#, orItem( )property in VB.NET, is used to retrieve the OrderID from theOriginalversion 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 ...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