FlexGrid for WinForms | ComponentOne
Filter / Filter Operations / Types of Filters
In This Topic
    Types of Filters
    In This Topic

    FlexGrid, not only provides built-in filters such as Column filter, Value filter, and Condition Filter but also lets you create your own custom filter. The built-in filters are provided through AllowFiltering property of Column class which lets you set the type of filter to be applied on a particular column. On the other hand, the custom filters make use of the IC1ColumnFilter interface and the IC1ColumnFilterEditor interface. This topic discusses the implementation of built-in and custom filters in detail.

    Column Filter

    Column filter is the default filter that is applied to all the columns automatically when AllowFiltering property of the grid is set to true. The ColumnFilter is a combination of ValueFilter and ConditionFilter (discussed below) and gives user an option to choose either of them at runtime. While working with code, you can use its ValueFilter and ConditionFilter properties to access the two types of filters. The filter also provides Apply method to apply filter to a value and Reset method to reset the filter and hence making it inactive.

    Column filter

    Use the below code to apply column filter on ProductName column of the WinForms FlexGrid.

    // Column Filter(Filters products whose ProductName begins with 'C')
    ColumnFilter colFilter = new ColumnFilter();
    colFilter.ConditionFilter.Condition1.Parameter = "C";
    colFilter.ConditionFilter.Condition1.Operator = ConditionOperator.BeginsWith;
    c1FlexGrid1.Cols["ProductName"].Filter = colFilter;    
    
        ' Column Filter(Filters products whose ProductName begins with 'C')
        Dim colFilter As ColumnFilter = New ColumnFilter()
        colFilter.ConditionFilter.Condition1.Parameter = "C"
        colFilter.ConditionFilter.Condition1.[Operator] = ConditionOperator.BeginsWith
        c1FlexGrid1.Cols("ProductName").Filter = colFilter
    

    Value Filter

    Value filter refers to the value based filter which gets enabled on setting the AllowFiltering property of column to ByValue. The ValueFilter provides the dropdown list of all values with checkbox which lets the user select the values which are to be displayed in the output. These values can be get or set through the ShowValues property. You can also set a limit on number of values to be displayed in the dropdown list by setting the ValuesLimit property. Just like column filter, value filter also provides Apply method to apply filter to a value and Reset method to reset the filter.

    Value filter

    Below code shows how to apply ValueFilter on a column of the WinForms FlexGrid.

    // Value Filter (Filters products whose CategoryId is 1,2,3,5,8)
    ValueFilter valueFilter = new ValueFilter();
    valueFilter.ShowValues = new object[] { 1, 2, 3, 5, 8 };
    c1FlexGrid1.Cols["CategoryId"].Filter = valueFilter;                
    
        ' Value Filter (Filters products whose CategoryId is 1,2,3,5,8)
        Dim valueFilter As ValueFilter = New ValueFilter()
        valueFilter.ShowValues = New Object() {1, 2, 3, 5, 8}
        c1FlexGrid1.Cols("CategoryId").Filter = valueFilter  
    

    Condition Filter

    Condition filter refers to a filter based on one or two logical conditions which can be enabled by setting the AllowFiltering property to ByCondition. The ConditionFilter gives user an option to set the condition/s through Condition1 and Condition2 properties which can be combined using AND/OR operator by setting the AndConditions property to filter the records. Similar to other filters, this class also provides Apply and Reset method.

    Condition filter

    Below code demonstrates how to apply a ConditionFilter on a WinForms FlexGrid column.

    // Condition Filter(Filters products whose UnitPrice >= 50 and <= 100)
    ConditionFilter conditionFilter = new ConditionFilter();
    conditionFilter.Condition1.Parameter = 50;
    conditionFilter.Condition1.Operator = ConditionOperator.GreaterThanOrEqualTo;
    conditionFilter.Condition2.Parameter = 100;
    conditionFilter.Condition2.Operator = ConditionOperator.LessThanOrEqualTo;
    conditionFilter.AndConditions = true;
    c1FlexGrid1.Cols["UnitPrice"].Filter = conditionFilter;                    
    
        ' Condition Filter(Filters products whose UnitPrice >= 50 and <= 100)
        Dim conditionFilter As ConditionFilter = New ConditionFilter()
        conditionFilter.Condition1.Parameter = 50
        conditionFilter.Condition1.[Operator] = ConditionOperator.GreaterThanOrEqualTo
        conditionFilter.Condition2.Parameter = 100
        conditionFilter.Condition2.[Operator] = ConditionOperator.LessThanOrEqualTo
        conditionFilter.AndConditions = True
        c1FlexGrid1.Cols("UnitPrice").Filter = conditionFilter         
    

    Custom Filter

    The abovementioned filters provide you enough flexibility to implement most common filtering scenarios efficiently. In addition, custom filter option also lets you create your own filter to meet any other specialized requirements of your application. To create a custom filter, you need to create a filter class that implements the IC1ColumnFilter interface, and an editor class that implements the IC1ColumnFilterEditor interface. By default, the default filters are also displayed along with the custom filter. However, you can choose to hide these default filters by setting ValueFilterEnabled and ConditionFilterEnabled properties to false. Additionally, you can easily export and import filter definitions using WriteXml and ReadXml methods, respectively. For proper implementation of custom column filters in FlexGrid, see Custom Column Filters.