ASPxGridView - How to apply Custom Function Filter Criteria Operator
ASPxGridView - How to apply Custom Function Filter Criteria Operator
Tags:
Some complicated filter cases require visiting every leaf of the filter criteria tree when a custom function criteria operator is used.
This example shows how it can be implemented via CriteriaPatchBase's class descendant.
The main idea is to operate with ASPxGridView's FilterExpression:
1. If FilterExpression has a custom function operator, it's necessary to find exactly this custom operator and replace its value with an actual one.
In the case of Auto Filter Row, this can be done in the ASPxGridView.ProcessColumnAutoFilter event handler as shown below:
[C#]
protected void Grid_ProcessColumnAutoFilter(object sender, DevExpress.Web.ASPxGridViewAutoFilterEventArgs e) { var grid = (ASPxGridView)sender; if(e.Column.FieldName == SpecialFilterColumnFieldName && e.Kind == GridViewAutoFilterEventKind.CreateCriteria) { grid.FilterExpression = UpdateGridFilterExpression(grid, e); e.Criteria = null; } }
Note that e.Criteria in this case must be set to null.
2. Then, assign the custom criteria operator value to the filter editor.
The right place of the code to do this is the ASPxGridView.AutoFilterCellEditorInitialize event handler. For example, it can be implemented in the following manner:
[C#]
protected void Grid_AutoFilterCellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e) { var grid = (ASPxGridView)sender; if(e.Column.FieldName == SpecialFilterColumnFieldName) { var gridCriteria = CriteriaOperator.Parse(grid.FilterExpression); e.Editor.Value = CriteriaVisitor.GetCustomFunctionOperatorValue(gridCriteria, e.Column.FieldName); } }
where CriteriaVisitor is CriteriaPatchBase's class descendant (see implementation details).
Description
The CriteriaVisitor class provides only two public static methods: **RemoveCustomFunction**and GetCustomFunctionOperatorValue.
The first one returns CriteriaOperator without a custom operator; the second one returns a string value of a custom function criteria operator.
There are also two overridden methods: VisitFunction and VisitGroup. They return specific values depending on the VisitorMode(LookingOnlyForCustomOperator or RemoveCustomOperator).
The IsMyCustomFunctionOperator method allows determining whether FunctionOperator is exactly that custom one (MyCustomFunctionOperator).
The MyCustomFunctionOperator class contains some custom filtering logic, which is implemented in the **Evaluate**method.
Files to look at:
• CriteriaPatcherBase.cs (VB: CriteriaPatcherBase.vb)
• CriteriaVisitor.cs (VB: CriteriaVisitor.vb)
• MyCustomFunctionOperator.cs (VB: MyCustomFunctionOperator.vb)
• Default.aspx (VB: Default.aspx)
• Default.aspx.cs (VB: Default.aspx.vb)