DataView Class
Represents a databindable, customized view of a DataTable for sorting, filtering, searching, editing, and navigation.
Assembly: System.Data (in System.Data.dll)
A major function of the DataView is to allow for 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 lets you have two controls bound to the same DataTable, but that show different versions of the data. For example, one control might be bound to a DataView that shows all the rows in the table, and a second might be configured to display only the rows that have been deleted from the DataTable. The DataTable also has a DefaultView property. This returns the default DataView for the table. For example, if you want 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.
Note: |
|---|
If you do not explicitly specify sort criteria for DataView, the DataRowView objects in DataView are sorted based on the index of DataView's corresponding DataRow in the DataTable.Rows DataRowCollection. |
LINQ to DataSet allows developers to create complex, powerful queries over a DataSet by using LINQ. A LINQ to DataSet query returns an enumeration of DataRow objects, however, which is not easily used in a binding scenario. DataView can be created from a LINQ to DataSet query and takes on the filtering and sorting characteristics of that query. LINQ to DataSet extends the functionality of the DataView by providing LINQ expression-based filtering and sorting, which allows for much more complex and powerful filtering and sorting operations than string-based filtering and sorting. See Data Binding and LINQ to DataSet for more information.
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.
Private Sub DemonstrateDataView() ' Create one DataTable with one column. Dim table As DataTable = New DataTable("table") Dim colItem As DataColumn = New DataColumn("item", _ Type.GetType("System.String")) table.Columns.Add(colItem) ' Add five items. Dim NewRow As DataRow Dim i As Integer For i = 0 To 4 NewRow = table.NewRow() NewRow("item") = "Item " & i table.Rows.Add(NewRow) Next table.AcceptChanges() ' Create two DataView objects with the same table. Dim firstView As DataView = New DataView(table) Dim secondView As DataView = New DataView(table) ' Change the values in the table. table.Rows(0)("item") = "cat" table.Rows(1)("item") = "dog" ' Print current table values. PrintTableOrView(table, "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 rowView As DataRowView rowView = secondView.AddNew() rowView("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 view As DataView, ByVal label As String) Console.WriteLine(label) Dim i As Integer For i = 0 To view.count - 1 Console.WriteLine(view(i)("item")) Next Console.WriteLine() End Sub Overloads Private Sub PrintTableOrView( _ ByVal table As DataTable, ByVal label As String) Console.WriteLine(label) Dim i As Integer For i = 0 To table.Rows.Count - 1 Console.WriteLine(table.Rows(i)("item")) Next Console.WriteLine() End Sub
The following example creates a DataView of online orders ordered by total due from a LINQ to DataSet query:
Dim orders As DataTable = dataSet.Tables("SalesOrderHeader") Dim query = _ From order In orders.AsEnumerable() _ Where order.Field(Of Boolean)("OnlineOrderFlag") = True _ Order By order.Field(Of Decimal)("TotalDue") _ Select order Dim view As DataView = query.AsDataView() bindingSource1.DataSource = view
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note: