Filtering in Code

  • 2 minutes to read

You can apply filters to the grid in code. Applying a filter to the grid clears all column filters (if any).

Applying a Filter to the Grid

To apply a filter to the grid, use the DataControlBase.FilterCriteria or DataControlBase.FilterString property. Both properties specify the grid’s filter, which can consist of multiple conditions applied to multiple columns.

When using the DataControlBase.FilterCriteria property you should create a CriteriaOperator object or its descendant, which represents the filter expression:

grid.FilterCriteria = (
  new BinaryOperator("OrderDate", new DateTime(1995, 1, 1), BinaryOperatorType.Less) &
  new BinaryOperator("UnitPrice", 10, BinaryOperatorType.Less)) |
  ( new BinaryOperator("OrderDate", new DateTime(1996, 1, 1), BinaryOperatorType.GreaterOrEqual) &
  new BinaryOperator("UnitPrice", 100, BinaryOperatorType.GreaterOrEqual));

Or you can use the CriteriaOperator.Parse method:

grid.FilterCriteria =
    CriteriaOperator.Parse("([OrderDate] < #1/1/1995# AND [UnitPrice] < 10)" +
    " OR ([OrderDate] >= #1/1/1996# AND [UnitPrice] >= 100)");

The DataControlBase.FilterString property allows you to specify the filter string:

grid.FilterString = "([OrderDate] < #1/1/1995# AND [UnitPrice] < 10)" +
    " OR ([OrderDate] >= #1/1/1996# AND [UnitPrice] >= 100)";

You can also use the DataControlBase.MergeColumnFilters method, which combines the specified filter with the grid’s current filter using the AND operator.

Clearing Filters

To clear the grid’s filter, set the DataControlBase.FilterCriteria property to null or the DataControlBase.FilterString property to an empty string.

To clear a column’s filter, use the DataControlBase.ClearColumnFilter method.

See Also

Filter Expressions