Export (0) Print
Expand All

Sorting and Filtering Data Using a DataView

The DataView provides several capabilities for sorting and filtering data in a DataTable:

  • Using the Sort property, you can 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 Sort property is a null reference or an empty string, and when the table has a primary key defined.
  • Using the RowFilter property, you can specify subsets of rows based on their column values. For details about valid expressions for the RowFilter property, 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, to achieve best performance use the Find or FindRows methods of the DataView rather than setting the RowFilter property. Setting the RowFilter property causes the index for the data to be rebuilt, 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 Searching a DataView.

  • Using the RowStateFilter property, you can 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 will expose 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 using the RowVersion property of the DataRowView.

    The following table shows the options for DataViewRowState.

    DataViewRowStateDescription
    CurrentRowsThe Current row version of all Unchanged, Added, and Modified rows. This is the default.
    AddedThe Current row version of all Added rows.
    DeletedThe Original row version of all Deleted rows.
    ModifiedCurrentThe Current row version of all Modified rows.
    ModifiedOriginalThe Original row version of all Modified rows.
    NoneNo rows.
    OriginalRowsThe Original row version of all Unchanged, Modified, and Deleted rows.
    UnchangedThe Current row version of all Unchanged rows.

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 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)
[C#]
DataView prodView = new DataView(prodDS.Tables["Products"],
                                 "UnitsInStock <= ReorderLevel",
                                 "SupplierID, ProductName",
                                 DataViewRowState.CurrentRows);

See Also

Creating and Using DataViews | DataViewRowState Enumeration | DataColumn.Expression Property | DataTable Class | DataView Class

Show:
© 2014 Microsoft