System.Data Namespace
Describes the version of data in a DataRow.
This enumeration has a FlagsAttribute that allows a bitwise combination of its member values.
Member Name |
Description |
Added |
A new row. |
CurrentRows |
Current rows including unchanged, new, and modified rows. |
Deleted |
A deleted row. |
ModifiedCurrent |
A current version, which is a modified version of original data ( see ModifiedOriginal ). |
ModifiedOriginal |
The original version ( although it has since been modified and is available as ModifiedCurrent ). |
None |
None. |
OriginalRows |
Original rows including unchanged and deleted rows. |
Unchanged |
An unchanged row. |
[ C#, VB ] In the following example DataTable is created with a single column. The data and is changed, and the RowStateFilter of the DataView is set to display different row sets, depending on the DataViewRowState.
private void DemonstrateRowState ( ) {
int i;
// create a DataTable with one column.
DataTable myTable = new DataTable ( "myTable" );
DataColumn myColumn = new DataColumn ( "myColumn" );
myTable.Columns.Add ( myColumn );
// add ten rows.
DataRow myRow;
for ( i = 0;i < 10 ; i++ ) {
myRow = myTable.NewRow ( );
myRow [ "myColumn" ] = "item " + i;
myTable.Rows.Add ( myRow );
}
myTable.AcceptChanges ( );
// create a DataView with the table.
DataView myView = new DataView ( myTable );
// change one row' s value:
myTable.Rows [ 1 ] [ "myColumn" ] = "Hello";
// add one row:
myRow = myTable.NewRow ( );
myRow [ "myColumn" ] = "World";
myTable.Rows.Add ( myRow );
// set the RowStateFilter to display only Added and modified rows.
myView.RowStateFilter =
DataViewRowState.Added | DataViewRowState.ModifiedCurrent;
// print those rows. Output = "Hello" "World";
PrintView ( myView, "ModifiedCurrent and Added" );
// set filter to display on originals of modified rows.
myView.RowStateFilter = DataViewRowState.ModifiedOriginal;
PrintView ( myView,"ModifiedOriginal" );
// delete three rows.
myTable.Rows [ 1 ].Delete ( );
myTable.Rows [ 2 ].Delete ( );
myTable.Rows [ 3 ].Delete ( );
// set the RowStateFilter to display only Added and modified rows.
myView.RowStateFilter = DataViewRowState.Deleted;
PrintView ( myView,"Deleted" );
// set filter to display only current.
myView.RowStateFilter = DataViewRowState.CurrentRows;
PrintView ( myView,"Current" );
// set filter to display only unchanged rows.
myView.RowStateFilter = DataViewRowState.Unchanged;
PrintView ( myView,"Unchanged" );
// set filter to display only original rows.
myView.RowStateFilter = DataViewRowState.OriginalRows;
PrintView ( myView,"OriginalRows" );
}
private void PrintView ( DataView dv,string label ) {
Response.Write ( "<br>" + label );
for ( int i = 0;i < dv.Count ; i++ ) {
Response.Write ( dv [ ] [ "myColumn" ] );
}
}
Private Sub DemonstrateRowState ( )
Dim i As Integer
' create a DataTable with one column.
Dim myTable As New DataTable ( "myTable" )
Dim myColumn As New DataColumn ( "myColumn" )
myTable.Columns.Add ( myColumn )
' add ten rows.
Dim myRow As DataRow
For i = 0 To 9
myRow = myTable.NewRow ( )
myRow ( "myColumn" ) = "item " + i.ToString ( )
myTable.Rows.Add ( myRow )
Next i
myTable.AcceptChanges ( )
' create a DataView with the table.
Dim myView As New DataView ( myTable )
' change one row' s value:
myTable.Rows ( 1 ) ( "myColumn" ) = "Hello"
' add one row:
myRow = myTable.NewRow ( )
myRow ( "myColumn" ) = "World"
myTable.Rows.Add ( myRow )
' set the RowStateFilter to display only Added and modified rows.
myView.RowStateFilter = _
DataViewRowState.Added Or DataViewRowState.ModifiedCurrent
' print those rows. Output = "Hello" "World";
PrintView ( myView, "ModifiedCurrent and Added" )
' set filter to display on originals of modified rows.
myView.RowStateFilter = DataViewRowState.ModifiedOriginal
PrintView ( myView, "ModifiedOriginal" )
' delete three rows.
myTable.Rows ( 1 ).Delete ( )
myTable.Rows ( 2 ).Delete ( )
myTable.Rows ( 3 ).Delete ( )
' set the RowStateFilter to display only Added and modified rows.
myView.RowStateFilter = DataViewRowState.Deleted
PrintView ( myView, "Deleted" )
' set filter to display only current.
myView.RowStateFilter = DataViewRowState.CurrentRows
PrintView ( myView, "Current" )
' set filter to display only unchanged rows.
myView.RowStateFilter = DataViewRowState.Unchanged
PrintView ( myView, "Unchanged" )
' set filter to display only original rows.
myView.RowStateFilter = DataViewRowState.OriginalRows
PrintView ( myView, "OriginalRows" )
End Sub
Private Sub PrintView ( dv As DataView, label As String )
Response.Write ( ControlChars.Cr + label )
Dim i As Integer
For i = 0 To dv.Count - 1
Response.Write ( dv ( i ) ( "myColumn" ) )
Next i
End Sub |
|
C# |
VB |
The DataViewRowState values are used either to retrieve a particular version of data from a DataRow, or to determine what versions exist.
Set the RowStateFilter property of the DataView to specify which version or versions of data you want to view.
You can use the Boolean operator Or with the values to get more than one version.
The DataTable uses DataViewRowState in the Select method.
DataRow DataView DataView Select