A newer version of this page is available. Switch to the current version.

Filtering in Code

  • 3 minutes to read

The GridControl allows you to apply filters in the UI and in code. This topic explains how to apply filters in code.

Refer to the Filtering topic to learn how to apply a filter in the UI.

Applying a Filter to the Grid

Use the DataControlBase.FilterCriteria or DataControlBase.FilterString property to apply a filter to the grid. This filter can consist of multiple conditions applied to multiple columns. The DataControlBase.FilterChanged event fires when a filter is applied.

You should create a CriteriaOperator object or its descendant representing the filter expression when using the DataControlBase.FilterCriteria property:

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));

You can apply filter criteria by parsing a filter string with the static 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 a filter string:

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

End users can modify filters that you set using the DataControlBase.FilterCriteria and DataControlBase.FilterString properties. Use the DataControlBase.FixedFilter property to specify filter criteria that cannot be modified.

Applying a Filter to a Column

Use any of the following approaches to apply a filter to a column:

Use the column's ColumnBase.IsFiltered property to identify whether a filter is applied to the column. If this property returns true, you can obtain the column's filter using the DataControlBase.GetColumnFilterCriteria or DataControlBase.GetColumnFilterString method.

Clearing Filters

Clear GridControl Filter

Set the DataControlBase.FilterCriteria property to null or the DataControlBase.FilterString property to an empty string to clear the GridControl's filter.

Clear Column Filter

Use the DataControlBase.ClearColumnFilter method to clear a column's filter.

See Also