ComponentOne FlexGrid for WinForms
Managing filters programmatically
Using the C1FlexGrid Control > C1FlexGrid Filtering > Managing filters programmatically

As we mentioned earlier in this document, setting the grid's AllowFiltering property to True is enough to enable column filtering on all columns. In many cases however, you may want finer control over filtering. This can be achieved by modifying the AllowFiltering and Filter properties on individual columns.

For example, the code below enables filtering but restricts the filtering to columns of type string:

To write code in C#

C#
Copy Code
// bind and configure grid
_flex.DataSource = dtProducts;
_flex.Cols["UnitPrice"].Format = "#,###.00";
 
// enable filtering
_flex.AllowFiltering = true;
 
// restrict filtering to columns of type 'string'
foreach (Column c in _flex.Cols)
{
    c.AllowFiltering = c.DataType == typeof(string)
        ? AllowFiltering.Default
        : AllowFiltering.None;
}

You can customize the filtering process further by creating filters and assigning them to columns, or by retrieving existing filters and modifying their properties. For example, the code below creates a ConditionFilter, configures it to select all items that start with the letter "C", and then assigns the new filter to the "ProductName" column:

To write code in C#

C#
Copy Code
// create a new ConditionFilter
var filter = new ConditionFilter();
 
// configure filter to select items that start with "C"
filter.Condition1.Operator = ConditionOperator.BeginsWith;
filter.Condition1.Parameter = "C";
 
// assign new filter to column "ProductName"
_flexCustom.Cols["ProductName"].Filter = filter;
See Also