System.Data Namespace DataView Class
Sets or retrieves the row state filter used in the DataView.
Script |
DataView.RowStateFilter [ = enumValue ] |
The property is read/write with no default value.
Only rows that have been deleted using the Delete method will have their RowStateFilter value set to Deleted. Those rows added using the AddNew method will similarly have the property set to New.
NOTE: Using the Remove method of the DataRowCollection class does not mean that a row will be marked as Deleted. Use the Delete method instead to ensure such rows can be viewed in the DataView.
New rows will also be visible when the RowStateFilter is set to ModifiedCurrent or CurrentRows.
Deleted rows will also be visible when the RowStateFilter is set to ModifiedOriginal and OriginalRows.
The following example initializes a DataTable with a single column, then changes the data and sets the RowStateFilter of the DataView 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 new and modified rows.
myView.RowStateFilter = DataViewRowState.New |
DataViewRowState.ModifiedCurrent;
// print those rows. Output = "Hello" "World";
PrintView ( myView, "ModifiedCurrent and New" );
// 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 new 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 ( "<br>" & label )
Dim i As Integer
For i = 0 To dv.Count - 1
Response.Write ( dv ( i ) ( "myColumn" ) )
Next i
End Sub |
|
C# |
VB |
DataView Members DataViewRowState AddNew Delete