Grid for ASP.NET Web Forms - How to programmatically change a column's filter criterion in the grid's filter expression
This example demonstrates how to programmatically change a column's filter criterion on the server side.
In this example, editors below the grid allow users to specify a filter criterion for a column. When a user clicks the Filter button, the criterion is applied. Filters applied to other columns do not change.
To modify a column's filter in code, you need to split the grid filter expression into column filter criteria. You can use the non-published CriteriaColumnAffinityResolver.SplitByColumnNames
method for this purpose. The method receives the filter expression as a parameter and returns a value of the Tuple<CriteriaOperator, IDictionary<string, CriteriaOperator>>
type. The tuple's first item is a criteria operator that cannot be parsed during the internal logic execution. Usually, it is null. The second item is a dictionary that stores parsed criteria operators and column names.
- Call the
SplitByColumnNames
method to get a dictionary of column filter criteria.
var criteria = CriteriaColumnAffinityResolver.SplitByColumnNames(CriteriaOperator.Parse(targetGrid.FilterExpression)).Item2;
- Change the criteria for the specified column.
CriteriaOperator co = null;
if (FieldName == "ProductName") {
value += "%";
co = new FunctionOperator("Like", new OperandProperty(FieldName), new OperandValue(value));
} else
co = new BinaryOperator(FieldName, value, BinaryOperatorType.Equal);
if (!criteria.Keys.Contains(FieldName))
criteria.Add(FieldName, co);
else
criteria[FieldName] = co;
- Set the new filter expression for the grid.
targetGrid.FilterExpression = CriteriaOperator.ToString(GroupOperator.And(criteria.Values));