Sorting and Filtering Data Using a DataView
Theprovides several ways of sorting and filtering data in a :
You can use theproperty to specify single or multiple column sort orders and include ASC (ascending) and DESC (descending) parameters.
You can use theproperty 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 Sort property is a null reference or an empty string, and when the table has a primary key defined.
You can use the Expression property of the class.property to specify subsets of rows based on their column values. For details about valid expressions for the RowFilter property, see the reference information for the
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 theor methods of the DataView to achieve best performance rather than setting the RowFilter property. Setting the RowFilter property rebuilds the index for the data, adding overhead to your application and decreasing performance. The RowFilter property is best used in a data-bound application where a bound control displays filtered results. The Find and FindRows methods leverage the current index without requiring the index to be rebuilt. For more information about the Find and FindRows methods, see .
You can use theproperty to specify which row versions to view. The DataView implicitly manages which row version to expose depending upon the RowState of the underlying row. For example, if the RowStateFilter is set to DataViewRowState.Deleted, the DataView exposes the Original row version of all Deleted rows because there is no Current row version. You can determine which row version of a row is being exposed by using the RowVersion property of the DataRowView.
The following table shows the options for DataViewRowState.
DataViewRowState options Description
The Current row version of all Unchanged, Added, and Modified rows. This is the default.
The Current row version of all Added rows.
The Original row version of all Deleted rows.
The Current row version of all Modified rows.
The Original row version of all Modified rows.
The Original row version of all Unchanged, Modified, and Deleted rows.
The Current row version of all Unchanged rows.
For more information about row states and row versions, see.
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.
DataView prodView = new DataView(prodDS.Tables["Products"], "UnitsInStock <= ReorderLevel", "SupplierID, ProductName", DataViewRowState.CurrentRows);