Tài liệu Accessing Deleted Rows in a DataTable - Pdf 87


[ Team LiB ]Recipe 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.Load
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;


deletedTextBox.Clear( );
}

private void deletedRadioButton_CheckedChanged(object sender,
System.EventArgs e)
{
// Filter the view to include only deleted rows.
dv.RowStateFilter = DataViewRowState.Deleted;
dataGrid.ReadOnly = true;

// Get the DataTable from the DataView.
DataTable dt = dv.Table;
// Filter using the DataTable RowState.
DataRow[] delRows = dt.Select(null, null, DataViewRowState.Deleted);

StringBuilder sb = new StringBuilder("Deleted Records:" +
Environment.NewLine);
// Iterate over the collection of deleted rows.
foreach(DataRow row in delRows)
sb.Append("Order ID: " + row["OrderID",
DataRowVersion.Original] + Environment.NewLine);

deletedTextBox.Text = sb.ToString( );
}
Discussion
ADO.NET manages the state of the rows while they are being modified. Rows are
assigned a state from the DataRowState enumeration described in Table 2-4
.
Table 2-4. DataRowState enumeration

ADO.NET maintains several versions of the data in each row while it is being modified
to allow the disconnected to be later reconciled with the data source. Table 2-5
describes
the DataRowVersion enumeration values.
Table 2-5. DataRowVersion enumeration
Value Description
Current Current value. This version does not exist for rows with a state of Deleted.
Default
Default value as determined by the DataRowState:

The Current version for rows with Added, Modified, or Unchanged
state

The Original version for rows with Deleted state

The Proposed value for rows with Detached state
Original Original value. This version does not exist for rows with a state of Added.
Proposed
Proposed value. This value exists during a row edit operation started either
implicitly or explicitly with the BeginEdit( ) method and for Detached rows.
The HasVersion( ) method of the DataRow object checks whether a particular row
version exists.
The DataViewRowState enumeration is used to retrieve a particular version of data or to
determine whether a version exists. It is used for this purpose by both the Select( )
method of the DataTable and by the RowStateFilter property of the DataView. You can
retrieve more than one version by using a Boolean OR of DataViewRowState values.
Table 2-6
describes the DataViewRowState enumeration values.
Table 2-6. DataViewRowState enumeration
Value Description
Recipe 2.6 Accessing Deleted Rows in a DataTable


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status