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 ColumnBasedDataSource that names each column you want to filter:
    sheet.AddDataSource(range, dataSource);
    Worksheet.DoFilter will throw if the range has no data source or if the data source is not column‑based.
  • Column IDs in FilterCondition.ColumnID must exist in ColumnBasedDataSource.Columns; otherwise a ColumnDefineNotFoundException is raised during BeginFilter.
  • Filtering hides rows by setting their height to 0. Any filter combination must call Worksheet.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: ColumnID must match a data source column name; ColumnIndex is assigned automatically during BeginFilter.
  • Value parsing (ComparisonType): default Auto tries numeric → datetime → text.
    • Numeric parsing accepts numbers, strings, StringBuilder, and percentages such as "100%" (0.0–1.0 range).
    • Datetime parsing accepts DateTime values and many string forms (yyyy/MM/dd, yyyyMMdd, yyyy-M, yyyy年M月d日, h:m[:s], and general DateTime.TryParse).
    • Equality on dates respects DateTimeValidPart inferred from the format (e.g., YYYY, YYYYMM, YYYYMMDD, HHMM, HHMMSS).
  • Comparison target (ComparisonDataKind): use CellDataKind.Value (default) to compare raw values or CellDataKind.DisplayText to compare formatted text. The sample UI switches date columns to DisplayText when matching the rendered string.
  • Array rules: ConditionOperator.Includes accepts any IEnumerable; set ComparisonType.Numeric to require numeric members.
  • Null/empty handling: empty strings and null are 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.

API Reference

ConditionalDataFilter

MemberTypeDescription
ConditionsConditionCollectionThe collection of filter rules
ErrorOnMismatchedTypeboolIf true, throw on type mismatch during comparison
Apply()voidEvaluate rules (raises OnApply)
OnApplyeventHandle to call DoFilter

ConditionCollection

MethodDescription
Add(FilterCondition item)Add a rule
AddRange(params FilterCondition[] items)Add multiple rules
Remove(FilterCondition item)Remove a rule
Clear()Remove all rules
Contains(FilterCondition item)Check if a rule exists
CountNumber of rules

FilterCondition

Constructor ParameterTypeDefaultDescription
columnIdstringrequiredColumn ID in the data source
opConditionOperatorrequiredComparison operator
valueobjectrequiredValue to compare against
comparisonDataKindCellDataKindValueCompare raw value or display text
comparisonTypeComparisonTypeAutoHow to parse the value
PropertyTypeDescription
ColumnIDstringColumn identifier
OperatorConditionOperatorComparison operator
ValueobjectComparison value
IsEnabledboolWhether this rule is active
ComparisonDataKindCellDataKindCompare against raw value or display text

ConditionOperator Enum

ValueSymbolDescription
Equals==Equal to
NotEquals!=Not equal to
StartsWith~*Starts with
EndsWith*~Ends with
Contains~Contains
NotContains!~Does not contain
GreaterThan>Greater than
EqualsOrGreaterThan>=Greater than or equal
LessThan<Less than
EqualsOrLessThan<=Less than or equal
IncludesINValue is in the collection

ComparisonType Enum

ValueDescription
AutoAutomatically detect type (numeric → datetime → text)
NumericForce numeric comparison
TextForce text comparison
DateTimeForce datetime comparison

CellDataKind Enum

ValueDescription
ValueCompare against the raw cell value
DisplayTextCompare against the formatted display text

DateTimeValidPart Enum

Controls date comparison granularity (inferred automatically from the value format):

ValueDescription
YYYYMMDD_HHMMSSFull date and time
YYYYYear only
YYYYMMYear and month
YYYYMMDDYear, month, and day
HHHour only
HHMMHour and minute
HHMMSSHour, minute, and second
YYYYMMDD_HHMMDate and time (no seconds)
Was this article helpful?