Conditional Filters
Use ConditionalDataFilter to hide rows whose values do not satisfy a set of column rules. Filters work against a ColumnBasedDataSource attached to a worksheet range and can be combined with other row filters (for example, free‑text search).
Prerequisites
- Attach a
ColumnBasedDataSourcethat names each column you want to filter:sheet.AddDataSource(range, dataSource);Worksheet.DoFilterwill throw if the range has no data source or if the data source is not column‑based. - Column IDs in
FilterCondition.ColumnIDmust exist inColumnBasedDataSource.Columns; otherwise aColumnDefineNotFoundExceptionis raised duringBeginFilter. - Filtering hides rows by setting their height to
0. Any filter combination must callWorksheet.DoFilter(range, filter);.
Create and manage rules
ConditionalDataFilter.Conditions holds the active rules. Add or clear items and toggle FilterCondition.IsEnabled to control what runs.
using unvell.ReoGrid;
using unvell.ReoGrid.Data;
using unvell.ReoGrid.Data.ConditionFilter;
var sheet = grid.CurrentWorksheet;
var (ds, range) = BuildColumnDataSource(); // returns ColumnBasedDataSource + its range
sheet.AddDataSource(range, ds);
var filter = new ConditionalDataFilter
{
ErrorOnMismatchedType = false, // throw on numeric/type mismatch if true
};
filter.Conditions.AddRange(
new FilterCondition("price", ConditionOperator.GreaterThan, 1000),
new FilterCondition("currency", ConditionOperator.Equals, "USD", CellDataKind.Value, ComparisonType.Text),
new FilterCondition("tradeDate", ConditionOperator.Equals, "2024-05") // month‑level match
);
filter.OnApply += (s, e) => sheet.DoFilter(range, filter);
filter.Apply(); // evaluates rules and hides rows
Key rule settings
- Operators (
ConditionOperator):Equals,NotEquals,StartsWith,EndsWith,Contains,NotContains,GreaterThan,EqualsOrGreaterThan,LessThan,EqualsOrLessThan,Includes(array membership). - Column:
ColumnIDmust match a data source column name;ColumnIndexis assigned automatically duringBeginFilter. - Value parsing (
ComparisonType): defaultAutotries numeric → datetime → text.- Numeric parsing accepts numbers, strings,
StringBuilder, and percentages such as"100%"(0.0–1.0range). - Datetime parsing accepts
DateTimevalues and many string forms (yyyy/MM/dd,yyyyMMdd,yyyy-M,yyyy年M月d日,h:m[:s], and generalDateTime.TryParse). - Equality on dates respects
DateTimeValidPartinferred from the format (e.g.,YYYY,YYYYMM,YYYYMMDD,HHMM,HHMMSS).
- Numeric parsing accepts numbers, strings,
- Comparison target (
ComparisonDataKind): useCellDataKind.Value(default) to compare raw values orCellDataKind.DisplayTextto compare formatted text. The sample UI switches date columns toDisplayTextwhen matching the rendered string. - Array rules:
ConditionOperator.Includesaccepts anyIEnumerable; setComparisonType.Numericto require numeric members. - Null/empty handling: empty strings and
nullare treated as equal during equality checks.
Apply filters alone or in combination
Call Worksheet.DoFilter(range, filter) when rules change. A typical pattern is to run it from the filter’s OnApply event so UI changes trigger a re‑apply. To combine with other filters (for example, TextSearchDataFilter), wrap them in MultipleDataFilter:
var condition = new ConditionalDataFilter();
var search = new TextSearchDataFilter();
var filters = new MultipleDataFilter();
filters.Filters.Add(condition);
filters.Filters.Add(search);
condition.OnApply += (s, e) => sheet.DoFilter(range, filters);
search.OnApply += (s, e) => sheet.DoFilter(range, filters);
When you change conditions (add/remove, update values, toggle IsEnabled), call Apply() to raise OnApply and rerun DoFilter.