DataView Class
Represents a databindable, customized view of a DataTable for sorting, filtering, searching, editing, and navigation.
Assembly: System.Data (in System.Data.dll)
The DataView type exposes the following members.
| Name | Description | |
|---|---|---|
![]() ![]() | DataView | Initializes a new instance of the DataView class. |
![]() ![]() | DataView(DataTable) | Initializes a new instance of the DataView class with the specified DataTable. |
![]() ![]() | DataView(DataTable, String, String, DataViewRowState) | Initializes a new instance of the DataView class with the specified DataTable, RowFilter, Sort, and DataViewRowState. |
| Name | Description | |
|---|---|---|
![]() ![]() | AllowDelete | Sets or gets a value that indicates whether deletes are allowed. |
![]() ![]() | AllowEdit | Gets or sets a value that indicates whether edits are allowed. |
![]() ![]() | AllowNew | Gets or sets a value that indicates whether the new rows can be added by using the AddNew method. |
![]() ![]() | ApplyDefaultSort | Gets or sets a value that indicates whether to use the default sort. The default sort is (ascending) by all primary keys as specified by PrimaryKey. |
![]() ![]() | Container | Gets the container for the component. (Inherited from MarshalByValueComponent.) |
![]() ![]() | Count | Gets the number of records in the DataView after RowFilter and RowStateFilter have been applied. |
![]() ![]() | DataViewManager | Gets the DataViewManager associated with this view. |
![]() ![]() | DesignMode | Gets a value indicating whether the component is currently in design mode. (Inherited from MarshalByValueComponent.) |
![]() ![]() | Events | Gets the list of event handlers that are attached to this component. (Inherited from MarshalByValueComponent.) |
![]() ![]() | IsInitialized | Gets a value that indicates whether the component is initialized. |
![]() ![]() | IsOpen | Gets a value that indicates whether the data source is currently open and projecting views of data on the DataTable. |
![]() ![]() | Item | Gets a row of data from a specified table. |
![]() ![]() | RowFilter | Gets or sets the expression used to filter which rows are viewed in the DataView. |
![]() ![]() | RowStateFilter | Gets or sets the row state filter used in the DataView. |
![]() ![]() | Site | Gets or sets the site of the component. (Inherited from MarshalByValueComponent.) |
![]() ![]() | Sort | Gets or sets the sort column or columns, and sort order for the DataView. |
![]() ![]() | Table | Gets or sets the source DataTable. |
| Name | Description | |
|---|---|---|
![]() ![]() | AddNew | Adds a new row to the DataView. |
![]() ![]() | BeginInit | Starts the initialization of a DataView that is used on a form or used by another component. The initialization occurs at runtime. |
![]() ![]() | Close | Closes the DataView. |
![]() ![]() | ColumnCollectionChanged | Occurs after a DataColumnCollection has been changed successfully. |
![]() ![]() | CopyTo | Copies items into an array. Only for Web Forms Interfaces. |
![]() ![]() | Delete | Deletes a row at the specified index. |
![]() ![]() | Dispose | Releases all resources used by the MarshalByValueComponent. (Inherited from MarshalByValueComponent.) |
![]() ![]() | Dispose(Boolean) | Disposes of the resources (other than memory) used by the DataView object. (Overrides MarshalByValueComponent.Dispose(Boolean).) |
![]() ![]() | EndInit | Ends the initialization of a DataView that is used on a form or used by another component. The initialization occurs at runtime. |
![]() ![]() | Equals(DataView) | Determines whether the specified DataView instances are considered equal. |
![]() ![]() | Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
![]() ![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from MarshalByValueComponent.) |
![]() ![]() | Find(Object) | Finds a row in the DataView by the specified sort key value. |
![]() ![]() | Find(Object()) | Finds a row in the DataView by the specified sort key values. |
![]() ![]() | FindRows(Object) | Returns an array of DataRowView objects whose columns match the specified sort key value. |
![]() ![]() | FindRows(Object()) | Returns an array of DataRowView objects whose columns match the specified sort key value. |
![]() ![]() | GetEnumerator | Gets an enumerator for this DataView. |
![]() ![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() ![]() | GetService | Gets the implementer of the IServiceProvider. (Inherited from MarshalByValueComponent.) |
![]() ![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() ![]() | IndexListChanged | Occurs after a DataView has been changed successfully. |
![]() ![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() ![]() | OnListChanged | Raises the ListChanged event. |
![]() ![]() | Open | Opens a DataView. |
![]() ![]() | Reset | Reserved for internal use only. |
![]() ![]() | ToString | Returns a String containing the name of the Component, if any. This method should not be overridden. (Inherited from MarshalByValueComponent.) |
![]() ![]() | ToTable | Creates and returns a new DataTable based on rows in an existing DataView. |
![]() ![]() | ToTable(String) | Creates and returns a new DataTable based on rows in an existing DataView. |
![]() ![]() | ToTable(Boolean, String()) | Creates and returns a new DataTable based on rows in an existing DataView. |
![]() ![]() | ToTable(String, Boolean, String()) | Creates and returns a new DataTable based on rows in an existing DataView. |
![]() ![]() | UpdateIndex | Reserved for internal use only. |
![]() ![]() | UpdateIndex(Boolean) | Reserved for internal use only. |
| Name | Description | |
|---|---|---|
![]() ![]() | Disposed | Adds an event handler to listen to the Disposed event on the component. (Inherited from MarshalByValueComponent.) |
![]() ![]() | Initialized | Occurs when initialization of the DataView is completed. |
![]() ![]() | ListChanged | Occurs when the list managed by the DataView changes. |
| Name | Description | |
|---|---|---|
![]() | AsParallel | Enables parallelization of a query. (Defined by ParallelEnumerable.) |
![]() | AsQueryable | Converts an IEnumerable to an IQueryable. (Defined by Queryable.) |
![]() ![]() | Cast(Of TResult) | Casts the elements of an IEnumerable to the specified type. (Defined by Enumerable.) |
![]() ![]() | OfType(Of TResult) | Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.) |
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 SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

