Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The DataView provides several ways of sorting and filtering data in a DataTable:
You can use the Sort property to specify single or multiple column sort orders and include ASC (ascending) and DESC (descending) parameters.
You can use the ApplyDefaultSort property to automatically create a sort order, in ascending order, based on the primary key column or columns of the table. ApplyDefaultSort only applies when the
Sortproperty is a null reference or an empty string, and when the table has a primary key defined.You can use the RowFilter property to specify subsets of rows based on their column values. For details about valid expressions for the
RowFilterproperty, see the reference information for the Expression property of the DataColumn class.If you want to return the results of a particular query on the data, as opposed to providing a dynamic view of a subset of the data, use the Find or FindRows methods of the
DataViewto achieve best performance rather than setting theRowFilterproperty. Setting theRowFilterproperty rebuilds the index for the data, adding overhead to your application and decreasing performance. TheRowFilterproperty is best used in a data-bound application where a bound control displays filtered results. TheFindandFindRowsmethods leverage the current index without requiring the index to be rebuilt. For more information about theFindandFindRowsmethods, see Finding Rows.You can use the RowStateFilter property to specify which row versions to view. The
DataViewimplicitly manages which row version to expose depending upon theRowStateof the underlying row. For example, if theRowStateFilteris set to DataViewRowState.Deleted, theDataViewexposes theOriginalrow version of allDeletedrows because there is noCurrentrow version. You can determine which row version of a row is being exposed by using theRowVersionproperty of the DataRowView.The following table shows the options for DataViewRowState.
DataViewRowState options Description CurrentRows The Currentrow version of all Unchanged, Added, andModifiedrows. This is the default.Added The Currentrow version of allAddedrows.Deleted The Originalrow version of allDeletedrows.ModifiedCurrent The Currentrow version of allModifiedrows.ModifiedOriginal The Originalrow version of allModifiedrows.None No rows. OriginalRows The Originalrow version of all Unchanged, Modified, andDeletedrows.Unchanged The Currentrow version of allUnchangedrows.
For more information about row states and row versions, see Row States and Row Versions.
The following code example creates a view that shows all the products where the number of units in stock is less than or equal to the reorder level, sorted first by supplier ID and then by product name.
Dim prodView As DataView = New DataView(prodDS.Tables("Products"), _
"UnitsInStock <= ReorderLevel", _
"SupplierID, ProductName", _
DataViewRowState.CurrentRows)
DataView prodView = new DataView(prodDS.Tables["Products"],
"UnitsInStock <= ReorderLevel",
"SupplierID, ProductName",
DataViewRowState.CurrentRows);