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. Below example shows the implementation of a custom string filter which lets you filter based on ranges.

    Custom filter

    Following is the code of a custom filter created for the WinForms FlexGrid. Note that the class implements IC1CustomFilter interface.


    class StringFilter : C1.Win.C1FlexGrid.IC1ColumnFilter
        List<Point> _ranges = new List<Point>();
        /// <summary>
        /// Gets the list of points that define the strings accepted by this fiter.
        /// </summary>
        public List<Point> Ranges
            get { return _ranges; }
        // filter is active if range list is not empty.
        public bool IsActive
            get { return _ranges.Count > 0; }
        // reset filter.
        public void Reset()
        // apply filter to a given date
        public bool Apply(object value)
            if (value != null)
                var s = value.ToString();
                if (s.Length > 0)
                    int c = char.ToUpperInvariant(s[0]);
                    foreach (var cr in _ranges)
                        if (c >= char.ToUpperInvariant((char)cr.X) &&
                            c <= char.ToUpperInvariant((char)cr.Y))
                            return true;
            return false;
        // return editor control for this filter
        public C1.Win.C1FlexGrid.IC1ColumnFilterEditor GetEditor()
            return new StringFilterEditor();
    Friend Class StringFilter
        Implements C1.Win.C1FlexGrid.IC1ColumnFilter
        Private _ranges As List(Of Point) = New List(Of Point)()
        ''' <summary>
        ''' Gets the list of points that define the strings accepted by this fiter.
        ''' </summary>
        Public ReadOnly Property Ranges As List(Of Point)
                Return _ranges
            End Get
        End Property
        ' filter is active if range list is not empty.
        Public ReadOnly Property IsActive As Boolean Implements C1.Win.C1FlexGrid.IC1ColumnFilter.IsActive
                Return _ranges.Count > 0
            End Get
        End Property
        ' reset filter.
        Public Sub Reset() Implements C1.Win.C1FlexGrid.IC1ColumnFilter.Reset
        End Sub
        ' apply filter to a given date
        Public Function Apply(ByVal value As Object) As Boolean Implements C1.Win.C1FlexGrid.IC1ColumnFilter.Apply
            If value IsNot Nothing Then
                Dim s = value.ToString()
                If s.Length > 0 Then
                    Dim c As Integer = AscW(Char.ToUpperInvariant(s(0)))
                    For Each cr In _ranges
                        If c >= AscW(Char.ToUpperInvariant(Microsoft.VisualBasic.ChrW(cr.X))) AndAlso c <= AscW(Char.ToUpperInvariant(Microsoft.VisualBasic.ChrW(cr.Y))) Then
                            Return True
                        End If
                End If
            End If
            Return False
        End Function
        ' return editor control for this filter
        Public Function GetEditor() As C1.Win.C1FlexGrid.IC1ColumnFilterEditor Implements C1.Win.C1FlexGrid.IC1ColumnFilter.GetEditor
            Return New StringFilterEditor()
        End Function
    End Class

    Following is the code of a custom class that implements IC1CustomFilterEditor interface.


    public partial class StringFilterEditor : 
        StringFilter _filter;
        public StringFilterEditor()
        public void Initialize(C1.Win.C1FlexGrid.C1FlexGridBase grid, int columnIndex, C1.Win.C1FlexGrid.IC1ColumnFilter filter)
            _filter = (StringFilter)filter;
            // initialize checkbox values
            foreach (var pt in _filter.Ranges)
                switch ((char)pt.X)
                    case 'A':
                        _chkAE.Checked = true;
                    case 'F':
                        _chkFJ.Checked = true;
                    case 'K':
                        _chkKO.Checked = true;
                    case 'P':
                        _chkPT.Checked = true;
                    case 'U':
                        _chkUZ.Checked = true;
        public void ApplyChanges()
            // reset filter
            // add selected ranges
            foreach (Control ctl in this.Controls)
                var cb = ctl as CheckBox;
                if (cb != null && cb.Checked)
                    var pt = new Point((int)cb.Text[0], (int)cb.Text[cb.Text.Length - 1]);
        public bool KeepFormOpen
            get { return false; }
        void _chkAE_CheckedChanged(object sender, EventArgs e)
            var cb = sender as CheckBox;
            cb.Font = new Font(Font, cb.Checked ? FontStyle.Bold : FontStyle.Regular);
    Public Partial Class StringFilterEditor
        Inherits UserControl
        Implements C1.Win.C1FlexGrid.IC1ColumnFilterEditor
        Private _filter As StringFilter
        Public Sub New()
        End Sub
        Public Sub Initialize(ByVal grid As C1.Win.C1FlexGrid.C1FlexGridBase, ByVal columnIndex As Integer, ByVal filter As C1.Win.C1FlexGrid.IC1ColumnFilter) Implements C1.Win.C1FlexGrid.IC1ColumnFilterEditor.Initialize
            _filter = CType(filter, StringFilter)
            ' initialize checkbox values
            For Each pt In _filter.Ranges
                Select Case Microsoft.VisualBasic.ChrW(pt.X)
                    Case "A"c
                        _chkAE.Checked = True
                    Case "F"c
                        _chkFJ.Checked = True
                    Case "K"c
                        _chkKO.Checked = True
                    Case "P"c
                        _chkPT.Checked = True
                    Case "U"c
                        _chkUZ.Checked = True
                End Select
        End Sub
        Public Sub ApplyChanges() Implements C1.Win.C1FlexGrid.IC1ColumnFilterEditor.ApplyChanges
            ' reset filter
            ' add selected ranges
            For Each ctl As Control In Controls
                Dim cb = TryCast(ctl, CheckBox)
                If cb IsNot Nothing AndAlso cb.Checked Then
                    Dim pt = New Point(Microsoft.VisualBasic.AscW(cb.Text(0)), Microsoft.VisualBasic.AscW(cb.Text(cb.Text.Length - 1)))
                End If
        End Sub
        Public ReadOnly Property KeepFormOpen As Boolean Implements C1.Win.C1FlexGrid.IC1ColumnFilterEditor.KeepFormOpen
                Return False
            End Get
        End Property
        Private Sub _chkAE_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
            Dim cb = TryCast(sender, CheckBox)
            cb.Font = New Font(MyBase.Font, If(cb.Checked, FontStyle.Bold, FontStyle.Regular))
        End Sub
    End Class