Export (0) Print
Expand All
13 out of 32 rated this helpful - Rate this topic

DataView Class

Represents a databindable, customized view of a DataTable for sorting, filtering, searching, editing, and navigation.

For a list of all members of this type, see DataView Members.

System.Object
   System.ComponentModel.MarshalByValueComponent
      System.Data.DataView

[Visual Basic]
Public Class DataView
   Inherits MarshalByValueComponent
   Implements IBindingList, IList, ICollection, IEnumerable, _
   ITypedList, ISupportInitialize
[C#]
public class DataView : MarshalByValueComponent, IBindingList,
   IList, ICollection, IEnumerable, ITypedList, ISupportInitialize
[C++]
public __gc class DataView : public MarshalByValueComponent,
   IBindingList, IList, ICollection, IEnumerable, ITypedList,
   ISupportInitialize
[JScript]
public class DataView extends MarshalByValueComponent implements
   IBindingList, IList, ICollection, IEnumerable, ITypedList,
   ISupportInitialize

Thread Safety

This type is safe for multithreaded read operations. You must synchronize any write operations.

Remarks

A major function of the DataView is to allow data binding on both Windows Forms and Web Forms.

Additionally, a DataView can be customized to present a subset of data from the DataTable. This capability allows you to have two controls bound to the same DataTable, but showing different versions of the data. For example, one control may be bound to a DataView showing all of the rows in the table, while a second may be configured to display only the rows that have been deleted from the DataTable. The DataTable also has a DefaultView property which returns the default DataView for the table. For example, if you wish to create a custom view on the table, set the RowFilter on the DataView returned by the DefaultView.

To create a filtered and sorted view of data, set the RowFilter and Sort properties. Then use the Item property to return a single DataRowView.

You can also add and delete from the set of rows using the AddNew and Delete methods. When you use those methods, the RowStateFilter property can set to specify that only deleted rows or new rows be displayed by the DataView.

Example

[Visual Basic, C#, C++] The following example creates a single DataTable with one column and five rows. Two DataView objects are created and the RowStateFilter is set on each to show different views of the table data. The values are then printed.

[Visual Basic] 
Private Sub DemonstrateDataView()
   ' Create one DataTable with one column.
   Dim myTable As DataTable = New DataTable("myTable")
   Dim colItem As DataColumn = New DataColumn("item", Type.GetType("System.String"))
   myTable.Columns.Add(colItem)
   ' Add five items.
   Dim NewRow As DataRow
   Dim i As Integer
   For i = 0 To 4

      NewRow = myTable.NewRow()
      NewRow("item") = "Item " & i
      myTable.Rows.Add(NewRow)
   Next
   myTable.AcceptChanges()
   ' Create two DataView objects with the same table.
   Dim firstView As DataView = New DataView(myTable)
   Dim secondView As DataView = New DataView(myTable)

   ' Change the values in the table.
   myTable.Rows(0)("item") = "cat"
   myTable.Rows(1)("item") = "dog"

   ' Print current table values.
   PrintTableOrView(myTable, "Current Values in Table")
    
   ' Set first DataView to show only modified versions of original rows.
   firstView.RowStateFilter = DataViewRowState.ModifiedOriginal
   ' Print values.    
   PrintTableOrView(firstView, "First DataView: ModifiedOriginal")
   ' Add one New row to the second view.
   Dim myDataRowView As DataRowView
   myDataRowView = secondView.AddNew()
   myDataRowView("item") = "fish"
   ' Set second DataView to show modified versions of current rows, or New rows.
   secondView.RowStateFilter = DataViewRowState.ModifiedCurrent Or DataViewRowState.Added
   ' Print modified and Added rows.
   PrintTableOrView(secondView, "Second DataView: ModifiedCurrent or Added")
End Sub

Overloads Private Sub PrintTableOrView(ByVal dv As DataView, ByVal label As String)
   Console.WriteLine(label)
   Dim i As Integer
   For i = 0 To dv.count - 1

      Console.WriteLine(dv(i)("item"))
   Next
   Console.WriteLine()
End Sub

Overloads Private Sub PrintTableOrView(ByVal t As DataTable, ByVal label As String)
   Console.WriteLine(label)
   Dim i As Integer
   For i = 0 To t.Rows.Count - 1
      Console.WriteLine(t.Rows(i)("item"))
   Next
   Console.WriteLine()
End Sub

[C#] 
private void DemonstrateDataView(){
   // Create one DataTable with one column.
   DataTable myTable = new DataTable("myTable");
   DataColumn colItem = new DataColumn("item",Type.GetType("System.String"));
   myTable.Columns.Add(colItem);
   // Add five items.
   DataRow NewRow;
   for(int i = 0; i <5; i++){
      NewRow = myTable.NewRow();
      NewRow["item"] = "Item " + i;
      myTable.Rows.Add(NewRow);
   }
   // Change the values in the table.
   myTable.Rows[0]["item"]="cat";
   myTable.Rows[1]["item"] = "dog";
   myTable.AcceptChanges();

   // Create two DataView objects with the same table.
   DataView firstView = new DataView(myTable);
   DataView secondView = new DataView(myTable);

   // Print current table values.
   PrintTableOrView(myTable,"Current Values in Table");
   
   // Set first DataView to show only modified versions of original rows.
   firstView.RowStateFilter=DataViewRowState.ModifiedOriginal ;
   // Print values.   
   PrintTableOrView(firstView,"First DataView: ModifiedOriginal");
   // Add one New row to the second view.
   DataRowView myDataRowView;
   myDataRowView=secondView.AddNew();
   myDataRowView["item"] = "fish";
   // Set second DataView to show modified versions of current rows, or New rows.
   secondView.RowStateFilter=DataViewRowState.ModifiedCurrent | DataViewRowState.Added;
   // Print modified and Added rows.
   PrintTableOrView(secondView, "Second DataView: ModifiedCurrent | Added");
}

private void PrintTableOrView(DataTable t, string label){
   // This function prints values in the table or DataView.
   Console.WriteLine("\n" + label);
   for(int i = 0; i<t.Rows.Count;i++){
      Console.WriteLine("\t" + t.Rows[i]["item"]);
   }
   Console.WriteLine();
}

private void PrintTableOrView(DataView dv, string label){

   // This overload prints values in the table or DataView.
   Console.WriteLine("\n" + label);
   for(int i = 0; i<dv.Count;i++){
      Console.WriteLine("\t" + dv[i]["item"]);
   }
   Console.WriteLine();
}

[C++] 
private:
 void DemonstrateDataView(){
    // Create one DataTable with one column.
    DataTable* myTable = new DataTable(S"myTable");
    DataColumn* colItem = new DataColumn(S"item",Type::GetType(S"System.String"));
    myTable->Columns->Add(colItem);
    // Add five items.
    DataRow* NewRow;
    for(int i = 0; i <5; i++){
       NewRow = myTable->NewRow();
       NewRow->Item[S"item"] = String::Format( S"Item {0}", __box(i));
       myTable->Rows->Add(NewRow);
    }
    // Change the values in the table.
    myTable->Rows->Item[0]->Item[S"item"]=S"cat";
    myTable->Rows->Item[1]->Item[S"item"] = S"dog";
    myTable->AcceptChanges();
 
    // Create two DataView objects with the same table.
    DataView* firstView = new DataView(myTable);
    DataView* secondView = new DataView(myTable);
 
    // Print current table values.
    PrintTableOrView(myTable,S"Current Values in Table");
    
    // Set first DataView to show only modified versions of original rows.
    firstView->RowStateFilter=DataViewRowState::ModifiedOriginal ;
    // Print values.   
    PrintTableOrView(firstView,S"First DataView: ModifiedOriginal");
    // Add one New row to the second view.
    DataRowView* myDataRowView;
    myDataRowView=secondView->AddNew();
    myDataRowView->Item[S"item"] = S"fish";
    // Set second DataView to show modified versions of current rows, or New rows.
    secondView->RowStateFilter= static_cast<DataViewRowState>
       ( DataViewRowState::ModifiedCurrent | DataViewRowState::Added );
    // Print modified and Added rows.
    PrintTableOrView(secondView, S"Second DataView: ModifiedCurrent | Added");
 }
 
 void PrintTableOrView(DataTable* t, String* label){
    // This function prints values in the table or DataView.
    Console::WriteLine(S"\n{0}", label);
    for(int i = 0; i<t->Rows->Count;i++){
       Console::WriteLine(S"\t{0}", t->Rows->Item[i]->Item[S"item"]);
    }
    Console::WriteLine();
 }
 
 void PrintTableOrView(DataView* dv, String* label){
 
    // This overload prints values in the table or DataView.
    Console::WriteLine(S"\n{0}", label);
    for(int i = 0; i<dv->Count;i++){
       Console::WriteLine(S"\t{0}", dv->Item[i]->Item[S"item"]);
    }
    Console::WriteLine();
 }

[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

Namespace: System.Data

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

Assembly: System.Data (in System.Data.dll)

See Also

DataView Members | System.Data Namespace | DataSet | DataTable | DataViewManager

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.