Filtering in Code
- 2 minutes to read
In code, you can apply filters to a grid and individual columns. Applying a filter to a grid clears all column filters (if any).
#Applying a Filter to the Grid
To apply a filter to a 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, representing the filter expression:
using DevExpress.Data.Filtering;
// ...
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 a filter string.
grid.FilterString = "([OrderDate] < #1/1/1995# AND [UnitPrice] < 10)" +
" OR ([OrderDate] >= #1/1/1996# AND [UnitPrice] >= 100)";
#Applying a Filter to a Column
To apply a filter to a column, use the ColumnBase.AutoFilterValue property. The type of the comparison operator used to create filter conditions for a current column is specified by the ColumnBase.AutoFilterCondition property. You can also use the DataControlBase.MergeColumnFilters method, which combines a specified filter with a grid's current filter with an AND operator.
To identify whether a filter is applied to a column or not, use the column's ColumnBase.IsFiltered property. If this property returns true, you can obtain the column's filter using the DataControlBase.GetColumnFilterCriteria or DataControlBase.GetColumnFilterString method.
#Clearing Filters
To clear a 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.