Export (0) Print
Expand All

DataView.RowStateFilter Property

Gets or sets the row state filter used in the DataView.

[Visual Basic]
Public Property RowStateFilter As DataViewRowState
[C#]
public DataViewRowState RowStateFilter {get; set;}
[C++]
public: __property DataViewRowState get_RowStateFilter();
public: __property void set_RowStateFilter(DataViewRowState);
[JScript]
public function get RowStateFilter() : DataViewRowState;
public function set RowStateFilter(DataViewRowState);

Property Value

One of the DataViewRowState values.

Remarks

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 Added.

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.

Example

[Visual Basic, C#, C++] The following example creates 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.

[Visual Basic] 
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)
    Console.WriteLine(ControlChars.Cr + label)
    Dim i As Integer
    For i = 0 To dv.Count - 1
        Console.WriteLine(dv(i)("myColumn"))
    Next i
End Sub

[C#] 
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){
   Console.WriteLine("\n" + label);
   for(int i = 0;i < dv.Count ;i++) {
      Console.WriteLine(dv[i]["myColumn"]);
   }
}

[C++] 
private:
 void DemonstrateRowState(){
    int i;
    // Create a DataTable with one column.
    DataTable* myTable = new DataTable(S"myTable");
    DataColumn* myColumn = new DataColumn(S"myColumn");
    myTable->Columns->Add(myColumn);
    // Add ten rows.
    DataRow* myRow;
    for(i = 0;i < 10 ;i++) {
       myRow = myTable->NewRow();
       myRow->Item[S"myColumn"] = String::Format( S"item {0}", __box(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->Item[1]->Item[S"myColumn"] = S"Hello";
    // Add one row:
    myRow = myTable->NewRow();
    myRow->Item[S"myColumn"] = S"World";
    myTable->Rows->Add(myRow);
    // Set the RowStateFilter to display only Added and modified rows.
    myView->RowStateFilter= static_cast<DataViewRowState>
       (DataViewRowState::Added | DataViewRowState::ModifiedCurrent);
    // Print those rows. Output = "Hello" "World";
    PrintView(myView, S"ModifiedCurrent and Added");
    // Set filter to display on originals of modified rows.
    myView->RowStateFilter=DataViewRowState::ModifiedOriginal;
    PrintView(myView,S"ModifiedOriginal");
    // Delete three rows.
    myTable->Rows->Item[1]->Delete();
    myTable->Rows->Item[2]->Delete();
    myTable->Rows->Item[3]->Delete();
    // Set the RowStateFilter to display only Added and modified rows.
    myView->RowStateFilter=DataViewRowState::Deleted;
    PrintView(myView,S"Deleted");
    //Set filter to display only current.
    myView->RowStateFilter=DataViewRowState::CurrentRows;
    PrintView(myView,S"Current");
    // Set filter to display only unchanged rows.
    myView->RowStateFilter=DataViewRowState::Unchanged; 
    PrintView(myView,S"Unchanged");
    // Set filter to display only original rows.
    myView->RowStateFilter=DataViewRowState::OriginalRows ; 
    PrintView(myView,S"OriginalRows");
 }
 
 void PrintView(DataView* dv,String* label){
    Console::WriteLine(S"\n{0}", label);
    for(int i = 0;i < dv->Count ;i++) {
       Console::WriteLine(dv->Item[i]->Item[S"myColumn"]);
    }
 }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

DataView Class | DataView Members | System.Data Namespace | DataViewRowState | AddNew | Delete

Show:
© 2014 Microsoft