ComponentOne FlexGrid for WinForms
Customizing the Filtering UI
Using the C1FlexGrid Control > C1FlexGrid Filtering > Customizing the Filtering UI

We believe the default filtering behavior and UI will address the vast majority of scenarios that involve column filtering. However, you can use the column filter classes independently, implementing your own custom user interface.

For example, the code below shows how you can use the ConditionFilter class to implement an iTunes-style search box for the C1FlexGrid. This type of search allows users to type in a value and automatically filters the grid rows to show the rows that contain the search string in any column.

To implement the iTunes-style search, we start with a text box that contains the text that will be used as a filter parameter. We also define a timer that will apply the filter a few milliseconds after the users stops typing into the text box:

To write code in C#

C#
Copy Code
public Form1()
{
    InitializeComponent();
 
    // configure timer to apply the filter 1/2 second after the
    // user stops typing into the search box
    _timer.Interval = 500;
    _timer.Tick += t_Tick;
 
    // monitor changes to the search box
    _txtSearch.TextChanged += _txtSearch_TextChanged;
}
 
// re-start timer when the text in the search box changes
void _txtSearch_TextChanged(object sender, EventArgs e)
{
    _timer.Stop();
    _timer.Start();
}

Now that the timer is configured, all we need to do is create and apply the filter when the timer ticks:

To write code in C#

C#
Copy Code
// apply filter when the timer ticks
void t_Tick(object sender, EventArgs e)
{
    // done for now...
    _timer.Stop();
 
    // configure filter
    var filter = new C1.Win.C1FlexGrid.ConditionFilter();
    filter.Condition1.Operator = C1.Win.C1FlexGrid.ConditionOperator.Contains;
    filter.Condition1.Parameter = _txtSearch.Text;
 
    // apply filter
    _flex.BeginUpdate();
    for (int r = _flex.Rows.Fixed; r < _flex.Rows.Count; r++)
    {
        bool visible = false;
        for (int c = _flex.Cols.Fixed; c < _flex.Cols.Count; c++)
        {
            if (filter.Apply(_flex[r, c]))
            {
                visible = true;
                break;
            }
        }
        _flex.Rows[r].Visible = visible;
    }
    _flex.EndUpdate();
}
See Also