Skip to main content

Filtering in Code

  • 5 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 for information on how to apply a filter in the UI.

Apply Filters

Use the following techniques to apply a filter in code:

  • Assign a filter string to the DataControlBase.FilterString property. Refer to the Filter Expressions and Criteria Language Syntax topics for information on supported syntax.

    <dxg:GridControl FilterString="([OrderDate] &lt; #1/1/1995# AND [UnitPrice] &lt; 10) 
        OR ([OrderDate] &gt;= #1/1/1996# AND [UnitPrice] &gt;= 100)" /> 
    
    grid.FilterString = "([OrderDate] < #1/1/1995# AND [UnitPrice] < 10)" +
        " OR ([OrderDate] >= #1/1/1996# AND [UnitPrice] >= 100)";
    
  • Create a CriteriaOperator object or its descendant that represents a filter expression and assign the created object to the DataControlBase.FilterCriteria property. Refer to the Criteria Operators topic for information on supported syntax.

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

Note

Use a column’s FieldName value to specify a filter expression with the DataControlBase.FilterCriteria / DataControlBase.FilterString property. Refer to How the GridControl Identifies Columns for more information.

The GridControl raises the DataControlBase.FilterChanged event when a filter is applied.

If the GridControl has a column filter, you can apply a filter to another column without resetting the existing filter. Pass a filter string or a CriteriaOperator to the DataControlBase.MergeColumnFilters method. This method uses the AND operator to combine filters for different columns.

Use the column’s ColumnBase.IsFiltered property to identify whether a filter is applied to the column. The DataControlBase.GetColumnFilterCriteria and DataControlBase.GetColumnFilterString methods allow you to obtain the column’s filter.

Users can modify filters specified by the DataControlBase.FilterCriteria and DataControlBase.FilterString properties. Use the DataControlBase.FixedFilter property to specify filter criteria that cannot be modified.

Data Analysis Filters

The GridControl allows you to apply the following Data Analysis Filters:

  • Top N
  • Bottom N
  • Above Average
  • Below Average
  • Unique
  • Duplicate

Use the DataControlBase.FilterString property to apply these filters in code:

grid.FilterString = "[#TopItems]([Profit], 10)"; // Profit in Top 10 Items
grid.FilterString = "[#TopPercent]([Profit], 10)"; // Profit in Top 10%
grid.FilterString = "[#BottomItems]([Profit], 10)"; // Profit in Bottom 10 Items
grid.FilterString = "[#BottomPercent]([Profit], 10)"; // Profit in Bottom 10%
grid.FilterString = "[#AboveAverage]([Profit])"; // Profit Above Average
grid.FilterString = "[#BelowAverage]([Profit])"; // Profit Below Average
grid.FilterString = "[#Unique]([Profit])"; // Profit is Unique
grid.FilterString = "[#Duplicate]([Profit])"; // Profit is Duplicate

You can also use the DataControlBase.FilterCriteria property:

grid.FilterCriteria = new FunctionOperator("#TopItems", new OperandProperty("Profit"), new OperandValue(10)); // Profit in Top 10 Items
grid.FilterCriteria = new FunctionOperator("#TopPercent", new OperandProperty("Profit"), new OperandValue(10)); // Profit in Top 10%
grid.FilterCriteria = new FunctionOperator("#BottomItems", new OperandProperty("Profit"), new OperandValue(10)); // Profit in Bottom 10 Items
grid.FilterCriteria = new FunctionOperator("#BottomPercent", new OperandProperty("Profit"), new OperandValue(10)); // Profit in Bottom 10%
grid.FilterCriteria = new FunctionOperator("#AboveAverage", new OperandProperty("Profit")); // Profit Above Average
grid.FilterCriteria = new FunctionOperator("#BelowAverage", new OperandProperty("Profit")); // Profit Below Average
grid.FilterCriteria = new FunctionOperator("#Unique", new OperandProperty("Profit")); // Profit is Unique
grid.FilterCriteria = new FunctionOperator("#Duplicate", new OperandProperty("Profit")); // Profit is Duplicate 

Conditional Formatting Filters

The GridControl allows you to apply Conditional Formatting Filters. These filters are based on the conditional formats specified in the GridControl.

Here is the XAML code that specifies a conditional format:

<dxg:GridControl x:Name="grid">
    <dxg:GridControl.View>
        <dxg:TableView>
            <dxg:TableView.FormatConditions>
                <dxg:TopBottomRuleFormatCondition FieldName="Profit" 
                    Rule="TopItems" Threshold="5" PredefinedFormatName="BoldText" />                
            </dxg:TableView.FormatConditions>            
        </dxg:TableView>
    </dxg:GridControl.View>
</dxg:GridControl> 

The code sample below applies a conditional formatting filter:

grid.FilterString = "[@TopItems]([Profit], 5)"; 
// or
grid.FilterCriteria = new FunctionOperator("@TopItems", new OperandProperty("Profit"), new OperandValue(5));

Clear Filters

Use the following techniques to clear filters:

See Also