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
| Member | Type | Description |
|---|
Conditions | ConditionCollection | The collection of filter rules |
ErrorOnMismatchedType | bool | If true, throw on type mismatch during comparison |
Apply() | void | Evaluate rules (raises OnApply) |
OnApply | event | Handle to call DoFilter |
ConditionCollection
| Method | Description |
|---|
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 |
Count | Number of rules |
FilterCondition
| Constructor Parameter | Type | Default | Description |
|---|
columnId | string | required | Column ID in the data source |
op | ConditionOperator | required | Comparison operator |
value | object | required | Value to compare against |
comparisonDataKind | CellDataKind | Value | Compare raw value or display text |
comparisonType | ComparisonType | Auto | How to parse the value |
| Property | Type | Description |
|---|
ColumnID | string | Column identifier |
Operator | ConditionOperator | Comparison operator |
Value | object | Comparison value |
IsEnabled | bool | Whether this rule is active |
ComparisonDataKind | CellDataKind | Compare against raw value or display text |
ConditionOperator Enum
| Value | Symbol | Description |
|---|
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 |
Includes | IN | Value is in the collection |
ComparisonType Enum
| Value | Description |
|---|
Auto | Automatically detect type (numeric → datetime → text) |
Numeric | Force numeric comparison |
Text | Force text comparison |
DateTime | Force datetime comparison |
CellDataKind Enum
| Value | Description |
|---|
Value | Compare against the raw cell value |
DisplayText | Compare against the formatted display text |
DateTimeValidPart Enum
Controls date comparison granularity (inferred automatically from the value format):
| Value | Description |
|---|
YYYYMMDD_HHMMSS | Full date and time |
YYYY | Year only |
YYYYMM | Year and month |
YYYYMMDD | Year, month, and day |
HH | Hour only |
HHMM | Hour and minute |
HHMMSS | Hour, minute, and second |
YYYYMMDD_HHMM | Date and time (no seconds) |