How to set Filter Criteria with multi conditions in C#
By FoxLearn 12/6/2024 9:54:17 AM 97
To achieve your goal of setting multi-condition filters in DevExpress GridControl, you can build the required filter expression using various CriteriaOperator class descendants.
GroupOperator
: Combines multiple conditions using logical operators like AND or OR.BinaryOperator
: Compares a column value to a constant (e.g.,Column > 100
).LikeOperator
: Used for text pattern matching (e.g.,Column LIKE '%ABC%'
).FunctionOperator
: Applies functions to column values (e.g.,Column1 > Function('NOW')
).
var criterion = new GroupOperator() { OperatorType = GroupOperatorType.Or }; criterion.Operands.Add(new FunctionOperator(FunctionOperatorType.StartsWith, new OperandProperty("Name"), new ConstantValue('r'))); criterion.Operands.Add(new BinaryOperator("Age", 10) { OperatorType = BinaryOperatorType.Equal }); criterion.Operands.Add(new BinaryOperator("Age", 5) { OperatorType = BinaryOperatorType.LessOrEqual }); gridView.FilterCriteria = criterion;
By combining these classes, you can create complex filter expressions and apply them to the GridView's ActiveFilterCriteria
property to filter data based on multiple conditions.
As an alternative, you can create a string representation of the required filter expression and then parse it using the CriteriaOperator.Parse
method. This method allows you to define the filter as a string (e.g., "Column1 > 100 AND Column2 LIKE '%ABC%'
") and then convert it into a CriteriaOperator
object, which can be applied to the ActiveFilterCriteria
property of the GridView.
gridView.FilterCriteria = CriteriaOperator.Parse("StartsWith([Name], 'r') OR [Age]=10 OR [Age]<= 5");
This approach simplifies the process of defining filters without manually constructing operator objects.