This documentation is archived and is not being maintained.

Filtering and Sorting Data Using Data Views

Sorting with a data view allows you to set sort criteria at design time and provides an object you can use for data binding. For an overview, see Introduction to Filtering and Sorting in Datasets.

You can filter and sort using a data view that you have explicitly added to a form or component. Doing this allows you to set filter and sort options at design time. Alternatively, you can use the default data view — called DefaultView — that is automatically available for every table in a dataset. When you use the default view, you can specify filter and sort options only in code.

To filter and sort using a data view

  1. If you want to set data view options at design time, add a data view to your form or component. For more information, see Adding Data Views to a Form or Component.
  2. Set the data view's Sort property using a sort expression. The sort expression can include the names of data table columns or a calculation. If you set the sort expression at run time, the data view reflects the change immediately.

    For more information about syntax for sort expressions, see DataColumn.Expression Property..

  3. Set the data view's RowFilter property using a filter expression. The filter expression should evaluate to true or false, as in this expression:
    CustomerStatus = 'Active'

    For more information about syntax for filter expressions, see DataColumn.Expression Property.

    Note   If a filter expression contains reserved characters, such as a single quotation mark, those characters must be specified using escape characters. For example, the following expression shows how to use an escape character to include an apostrophe in the expression:
    CompanyName = 'Margie\'s Travel'
  4. To filter based on a version or state of a record, set the RowStateFilter property to a value from the DataViewRowState Enumeration, such as the following:
    ' Visual Basic
    Dataview1.RowStateFilter = DataViewRowState.CurrentRows
    // C#
    Dataview1.RowStateFilter = DataViewRowState.CurrentRows;

The following example shows how to set the data view's filter and sort order at run time using the default data view:

' Visual Basic
Dataset1.Customers.DefaultView.Sort = "City"

// C#
Dataset1.Customers.DefaultView.Sort = "City";

The following example shows how to explicitly add a data view to a form. It checks the values of two radio buttons, and depending on which one is checked, sets a different sort order in the data view. The view filters only the current records. The results are displayed by binding an existing data grid to the data view.

' Visual Basic
If radioCity.Checked Then
    DataView1.Sort = "City"
ElseIf radioCompanyName.Checked Then
    DataView1.Sort = "CompanyName"
End If
Dataview1.RowFilter = "CustomerStatus = 'Active'"
Dataview1.RowStateFilter = DataViewRowState.CurrentRows
Me.DataGrid1.DataSource = DataView1

// C#
if (radioCity.Checked) 
   DataView1.Sort = "City";
else if (radioCompanyName.Checked)
   DataView1.Sort = "CompanyName";

DataView1.RowFilter = "CustomerStatus = 'Active'";
DataView1.RowStateFilter = DataViewRowState.CurrentRows;
// The following calls a different method to display the 
// data view contents.
this.dataGrid1.DataSource = DataView1;

See Also

Introduction to Filtering and Sorting in Datasets | Adding Data Views to a Form or Component | Filtering And Sorting Directly in Data Tables | Working with Records in Data Views | Data Binding and Windows Forms | Data Access in Web Forms Pages