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

Filter Editor

  • 9 minutes to read

The Filter Editor allows users to build filter criteria. They can add filter conditions and use logical operators to group filters.

NOTE

v19.1 and later use the new Filter Editor.

To use the previous Filter Editor:

The Server Mode and Virtual Sources use the previous Filter Editor.

Invoke the Filter Editor

Users can invoke the Filter Editor in the following ways:

  • Right-click the column's header and select Filter Editor...:

  • Click the Edit Filter button ( ) in the Filter Panel:

TIP

Set the DataViewBase.AllowFilterEditor property to false to prohibit users from invoking the Filter Editor.

To invoke the Filter Editor in code, call the DataViewBase.ShowFilterEditor method or the DataViewCommandsBase.ShowFilterEditor command:

<Button Content="Show Filter Editor" Command="{Binding Path=Commands.ShowFilterEditor, ElementName=view}" />

<!-- ... -->

<dxg:GridControl>            
    <dxg:GridControl.View>
        <dxg:TableView x:Name="view" />
    </dxg:GridControl.View>
</dxg:GridControl>

Filter Editor UI

The Filter Editor displays filter criteria as a tree structure where nodes are filter conditions. The Filter Editor groups filter conditions by logical operators if the filter criteria consist of several filter conditions.

Customize the Filter Editor

Use the DataViewBase.FilterEditorTemplate property to specify a custom data template. In the template, define the FilterEditorControl and handle its events.

Customize the Field List

TIP

Demo: Filter Editor - Customize the Field List

Requires installation of WPF Subscription. Download

The Filter Editor shows a list of the GridControl's fields. If the GridControl has Bands, the Filter Editor shows fields in a hierarchical structure:

TIP

The FilterEditorControl.PropertySelectorMode property specifies whether to show fields in List or Tree mode.

Use the FilterEditorControl.QueryFields event to customize the field list. The following code sample shows how to add the ShipCountry, ShipCity, ShipAddress fields to the Ship category:

<dxg:TableView x:Name="view">
    <dxg:TableView.FilterEditorTemplate>
        <DataTemplate>
            <dxfui:FilterEditorControl QueryFields="OnQueryFields" />
        </DataTemplate>
    </dxg:TableView.FilterEditorTemplate>
</dxg:TableView>
void OnQueryFields(object sender, QueryFieldsEventArgs e) {
    var shipCountry = e.Fields["ShipCountry"];
    var shipCity = e.Fields["ShipCity"];
    var shipAddress = e.Fields["ShipAddress"];

    shipCountry.Caption = "Country";
    shipCity.Caption = "City";
    shipAddress.Caption = "Address";

    e.Fields.Remove(shipCountry);
    e.Fields.Remove(shipCity);
    e.Fields.Remove(shipAddress);

    var shipGroup = new FieldItem { Caption = "Ship" };
    shipGroup.Children.Add(shipCountry);
    shipGroup.Children.Add(shipCity);
    shipGroup.Children.Add(shipAddress);
    e.Fields.Add(shipGroup);
} 

Customize the Operator List

Standard Operators

TIP

Demo: Filter Editor - Customize the Operator List

Requires installation of WPF Subscription. Download

The Filter Editor shows a list of operators the selected field accepts. Use the FilterEditorControl.QueryOperators event to customize the operator list.

The code sample below removes all operators except Equal and Not Equal:

<dxg:TableView x:Name="view">
    <dxg:TableView.FilterEditorTemplate>
        <DataTemplate>
            <dxfui:FilterEditorControl QueryOperators="OnQueryOperators" />
        </DataTemplate>
    </dxg:TableView.FilterEditorTemplate>
</dxg:TableView> 
void OnQueryOperators(object sender, FilterEditorQueryOperatorsEventArgs e) {
    if(e.FieldName == "OrderDate") {
        e.Operators.Clear();
        e.Operators.Add(new FilterEditorOperatorItem(FilterEditorOperatorType.Equal));
        e.Operators.Add(new FilterEditorOperatorItem(FilterEditorOperatorType.NotEqual));
    }
} 

Custom Operators

TIP

Demo: Filter Editor - Customize the Operator List

Requires installation of WPF Subscription. Download

You can use the FilterEditorControl.QueryOperators event to add custom operators. The code sample below adds the Last Years operator:

  1. Create a custom function. Do one of the following:

    • Use the CustomFunctionFactory.Create method.

      The CustomFunctionFactory is an extension of the DevExpress.Xpf.Grid.v19.1.Extensions.dll library. Refer to c:\Program Files (x86)\DevExpress 19.1\Components\Sources\DevExpress.Xpf.Grid\DevExpress.Xpf.Grid.Extensions\ for information on how extension methods work.

      The CustomFunctionFactory.Create method allows you to create a custom function with a maximum of 4 operands.

    • Implement the ICustomFunctionOperator interface. Refer to the Implementing Custom Functions topic for more information.

    const string CustomFunctionName = "LastYears";
    var currentYear = DateTime.Now.Year;
    
    ICustomFunctionOperatorBrowsable customFunction = CustomFunctionFactory.Create(CustomFunctionName, 
        (DateTime date, int threshold) => {
            return currentYear >= date.Year && currentYear - date.Year <= threshold;
        }
    );
    
  2. Call the CriteriaOperator.RegisterCustomFunction method to register the custom function.

    CriteriaOperator.RegisterCustomFunction(customFunction);
    
  3. Create the FilterEditorOperatorItem and add it to the FilterEditorQueryOperatorsEventArgs.Operators collection. Specify the operator item's edit settings to define its operands:

    void OnQueryOperators(object sender, FilterEditorQueryOperatorsEventArgs e) {
        if(e.FieldName == "OrderDate") {
            // ...
    
            var customFunctionEditSettings = new BaseEditSettings[] { 
                new TextEditSettings { MaskType = MaskType.Numeric, Mask = "D", MaskUseAsDisplayFormat = true } 
            };
            e.Operators.Add(new FilterEditorOperatorItem(CustomFunctionName, customFunctionEditSettings) { Caption = "Last Years" });
        }
    } 
    

Predefined Filters

You can specify Predefined Filters with the ColumnBase.PredefinedFilters property. The Filter Editor displays these filters in the Predefined filters submenu:

<dxg:GridColumn FieldName="UnitPrice">
    <dxg:GridColumn.PredefinedFilters>
        <dxfui:PredefinedFilterCollection>
            <dxfui:PredefinedFilter Name="Less than 10" Filter="?p &lt; 10" />
            <dxfui:PredefinedFilter Name="Between 10 and 50" Filter="?p &gt; 10 and ?p &lt; 50" />
            <dxfui:PredefinedFilter Name="Between 50 and 100" Filter="?p &gt; 50 and ?p &lt; 100" />
            <dxfui:PredefinedFilter Name="Greater than 100" Filter="?p &gt; 100" />
        </dxfui:PredefinedFilterCollection>
    </dxg:GridColumn.PredefinedFilters>
</dxg:GridColumn> 

Customize Operand Template

TIP

Demo: Filter Editor - Customize Operands

Requires installation of WPF Subscription. Download

The Filter Editor automatically creates operand editors based on the field and operator type. You can customize operand editors.

The following code sample specifies the TrackBarEdit as an operand for the Between and NotBetween operators:

  1. Create a template for the operands. The following models depend on the operator type. Use their properties to bind to operand values in the template:

    <UserControl.Resources>
        <DataTemplate x:Key="ternaryTemplate">
            <dxe:TrackBarEdit Minimum="0" Maximum="300" MinWidth="120" 
                              TickPlacement="None" dxfui:FilterEditorNavigation.Index="0">
                <dxe:TrackBarEdit.EditValue>
                    <MultiBinding Converter="{local:TrackBarEditRangeConverter}">
                        <Binding Path="Left"/>
                        <Binding Path="Right"/>
                    </MultiBinding>
                </dxe:TrackBarEdit.EditValue>
                <dxe:TrackBarEdit.StyleSettings>
                    <dxe:TrackBarRangeStyleSettings />
                </dxe:TrackBarEdit.StyleSettings>
            </dxe:TrackBarEdit>
        </DataTemplate>
    </UserControl.Resources>
    
    TrackBarEditRangeConverter
    public class TrackBarEditRangeConverter : BaseMultiValueConverter {
        public override object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) {
            if(values.Length != 2)
                return new TrackBarEditRange();
            return new TrackBarEditRange(GetApproptiateValue(values[0]), GetApproptiateValue(values[1]));
        }
    
        short GetApproptiateValue(object value) {
            if(value is int)
                return System.Convert.ToInt16(value);
            return value is short ? (short)value : (short)0;
        }
    
        public override object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) {
            var trackBarEditRange = value as TrackBarEditRange;
            if(trackBarEditRange == null)
                return new object[] { (short)0, (short)0 };
            return new object[] { (short)trackBarEditRange.SelectionStart, (short)trackBarEditRange.SelectionEnd };
        }
    } 
    
  2. Handle the FilterEditorControl.QueryOperators event, get the operator, and assign the created template to the OperatorItemBase.OperandTemplate property.

    <dxg:TableView x:Name="view">
        <dxg:TableView.FilterEditorTemplate>
            <DataTemplate>
                <dxfui:FilterEditorControl QueryOperators="OnQueryOperators" />
            </DataTemplate>
        </dxg:TableView.FilterEditorTemplate>
    </dxg:TableView> 
    
    void OnQueryOperators(object sender, FilterEditorQueryOperatorsEventArgs e) {
        if(e.FieldName == "Quantity") {
            var template = (DataTemplate)FindResource("ternaryTemplate");
            e.Operators[FilterEditorOperatorType.Between].OperandTemplate = template;
            e.Operators[FilterEditorOperatorType.NotBetween].OperandTemplate = template;
        }
    } 
    

Prohibit Operations

Prohibit Group Types

TIP

Demo: Filter Editor - Prohibit Group Types

Requires installation of WPF Subscription. Download

Use the FilterEditorControl.QueryGroupTypes event to prohibit group types. The following code sample prohibits users from specifying the Or and NotOr logical operators:

<dxg:TableView x:Name="view">
    <dxg:TableView.FilterEditorTemplate>
        <DataTemplate>
            <dxfui:FilterEditorControl QueryGroupTypes="OnQueryGroupTypes" />
        </DataTemplate>
    </dxg:TableView.FilterEditorTemplate>
</dxg:TableView> 
void OnQueryGroupTypes(object sender, QueryGroupTypesEventArgs e) {
    e.AllowNotAnd = false;
    e.AllowNotOr = false;
} 

Prohibit Group Operations

TIP

Demo: Filter Editor - Prohibit Group Operations

Requires installation of WPF Subscription. Download

Use the FilterEditorControl.QueryGroupOperations event to prohibit group operations. The following code sample prohibits users from adding custom expressions:

<dxg:TableView x:Name="view">
    <dxg:TableView.FilterEditorTemplate>
        <DataTemplate>
            <dxfui:FilterEditorControl QueryGroupOperations="OnQueryGroupOperations" />
        </DataTemplate>
    </dxg:TableView.FilterEditorTemplate>
</dxg:TableView> 
void OnQueryGroupOperations(object sender, QueryGroupOperationsEventArgs e) {
    e.AllowAddCustomExpression = false;
} 

Prohibit Removing Conditions

TIP

Demo: Filter Editor - Prohibit Condition Operations

Requires installation of WPF Subscription. Download

Use the FilterEditorControl.QueryConditionOperations event to prohibit users from removing conditions:

<dxg:TableView x:Name="view">
    <dxg:TableView.FilterEditorTemplate>
        <DataTemplate>
            <dxfui:FilterEditorControl QueryConditionOperations="OnQueryConditionOperations" />
        </DataTemplate>
    </dxg:TableView.FilterEditorTemplate>
</dxg:TableView> 
void OnQueryConditionOperations(object sender, QueryConditionOperationsEventArgs e) {
    e.AllowRemoveCondition = false;
} 

Standalone Filter Editor

TIP

Demo: Filter Editor - Standalone Filter Editor

Requires installation of WPF Subscription. Download

You can use a standalone FilterEditorControl to edit filters outside of the GridControl.

Specify the FilterEditorControl.Context property to associate the FilterEditorControl with the GridControl's filtering context:

The filter criteria specified in the FilterEditorControl are not automatically applied to the GridControl. To apply the current filter criteria, call the FilterEditorControl.ApplyFilter method or the FilterEditorCommands.ApplyFilter command.

<dxg:GridControl x:Name="filterGrid" ... />

<!-- ... -->

<dxfui:FilterEditorControl x:Name="filterEditor" Context="{Binding Path=FilteringContext, ElementName=filterGrid}"/>
<Button Content="Apply Filter" Command="{Binding Commands.ApplyFilter, ElementName=filterEditor}" />

TIP