New to Telerik UI for ASP.NET AJAXStart a free 30-day trial

Operating with RadListView FilterExpressions Manually

As of Q1 2010 Telerik RadListView has build-in server-side API for creating and applying filter expressions. It gives you the ability to filter the data displayed in a RadListView control without creating complex database queries.

Adding FilterExpressions for RadListView

RadListView provides an intuitive way for building filter expressions through its FilterExpressions Add() and BuildExpression() methods listed below:

MethodReturn TypeParametersDescription
Add()RadListViewFilterExpressionCollectionRadListViewFilterExpressionAdds the specified as an argument filter expression to the RadListView FilterExpressions collection.
BuildExpression()RadListViewFilterExpressionCollectionAction (optional)A helper method for building filter expression hierarchy in a fluent like manner.

RadListView FilterExpression Types

The following table describes all filter expression types you can use when building RadListView FilterExpressions:

FilterExpression typeDescription
RadListViewContainsFilterExpressionRepresents Contains RadListView filter expression.
RadListViewGreaterThanFilterExpressionRepresents GreaterThan RadListView filter expression where T is the type of the field on which filter will be applied.
RadListViewGreaterThanOrEqualToFilterExpressionRepresents GreaterThan or EqualTo RadListView filter expression where T is the type of the field on which filter will be applied.
RadListViewGroupFilterExpressionRepresents a group of filter expressions.
RadListViewIsEmptyFilterExpressionRepresents IsEmpty RadListView filter expression.
RadListViewIsNotEmptyFilterExpressionRepresents IsEmpty RadListView filter expression.
RadListViewIsNotNullFilterExpressionRepresents IsNotNull RadListView filter expression.
RadListViewIsNullFilterExpressionRepresents IsNull RadListView filter expression.
RadListViewLessThanFilterExpressionRepresents LessThan RadListView filter expression where T is the type of the field on which filter will be applied.
RadListViewLessThanOrEqualToFilterExpressionRepresents LessThan or EqualTo RadListView filter expression where T is the type of the field on which filter will be applied.
RadListViewEqualToFilterExpressionRepresents EqualTo RadListView filter expression where T is the type of the field on which filter will be applied.
RadListViewNotEqualToFilterExpressionRepresents NotEqualTo RadListView filter expression where T is the type of the field on which filter will be applied.
RadListViewStartsWithFilterExpressionRepresents StartsWith RadListView filter expression.
RadListViewEndsWithFilterExpressionRepresents EndsWith RadListView filter expression.

Here are some examples illustrating how you can use the above types to create filter expressions and add them to the RadListView FilterExpressions collection:

C#
RadListViewContainsFilterExpression expression1 = new
RadListViewContainsFilterExpression("CustomerID");
expression1.CurrentValue =
"ALFKI";
RadListView1.FilterExpressions.Add(expression1);
RadListView1.FilterExpressions.Add(new
RadListViewGreaterThanFilterExpression<DateTime>("ShippedDate") {
CurrentValue = DateTime.Parse("7/10/1996") });
RadListView1.FilterExpressions.Add(
    new
RadListViewGroupFilterExpression(RadListViewGroupFilterOperator.Or)
    {
        Expressions =
        {
            new
RadListViewGreaterThanFilterExpression<DateTime>("ShippedDate"){CurrentValue
= DateTime.Parse("7/10/1996")},
            new
RadListViewEqualToFilterExpression<int>("OrderID"){CurrentValue =
42}
        }
    }
);

BuildExpression() methods

Another possible approach for adding filter expressions to RadListView is using the BuildExpression() method and its methods listed here:

MethodParametersDescription
Contains(string fieldName, string currentValue)fieldName is the name of the field which will be filtered; currentValue is the value to be filtered onAdds a Contains filter expression
EqualTo(string fieldName, T currentValue)fieldName is the name of the field which will be filtered; currentValue is the value to be filtered onAdds an EqualTo filter expression where T is the type of the field which will be filtered
NotEqualTo(string fieldName, T currentValue)fieldName is the name of the field which will be filtered; currentValue is the value to be filtered onAdds a NotEqualTo filter expression where T is the type of the field which will be filtered
GreaterThan(string fieldName, T currentValue)fieldName is the name of the field which will be filtered; currentValue is the value to be filtered onAdds a GreaterThan filter expression where T is the type of the field which will be filtered
GreaterThanOrEqualTo(string fieldName, T currentValue)fieldName is the name of the field which will be filtered; currentValue is the value to be filtered onAdds a GreaterThanOrEqualTo filter expression where T is the type of the field which will be filtered
Group(Action groupBuilder)groupBuilder is the inner group instanceAdds a group of filter expressions
IsEmpty(string fieldName)fieldName is the name of the field which will be filteredAdds an IsEmpty filter expression
IsNotEmpty(string fieldName)fieldName is the name of the field which will be filteredAdds an IsNotEmpty filter expression
IsNull(string fieldName)fieldName is the name of the field which will be filteredAdds an IsNull filter expression
IsNotNull(string fieldName)fieldName is the name of the field which will be filteredAdds an IsNotNull filter expression
LessThan(string fieldName, T currentValue)fieldName is the name of the field which will be filtered; currentValue is the value to be filtered onAdds a LessThan filter expression where T is the type of the field which will be filtered
LessThanOrEqualTo(string fieldName, T currentValue)fieldName is the name of the field which will be filtered; currentValue is the value to be filtered onAdds a LessThanOrEqualTo filter expression where T is the type of the field which will be filtered
StartsWith(string fieldName, string currentValue)fieldName is the name of the field which will be filtered; currentValue is the value to be filtered onAdds a StartsWith filter
EndsWith(string fieldName, string currentValue)fieldName is the name of the field which will be filtered; currentValue is the value to be filtered onAdds a EndsWith filter
And()Adds an And operator between the filter expressions on its left and right side
Or()Adds an Or operator between the filter expressions on its left and right side

The following examples illustrate how you can use the BuildExpression() methods to create RadListView filter expressions:

C#
RadListView1.FilterExpressions.BuildExpression()
.GreaterThan("ShippedDate", DateTime.Parse("7/10/1996"))
.Or().EqualTo("OrderID", 42).Build();

When you are using the BuildExpression() methods to add filter expressions as shown above, you need to call the Build() at the end so the filter expressions are built and added to the RadListView FilterExpressions collection. Note that the Build() method could be called only once per expressions building. To check if the Build() method is already called, you can use the BuildExpression().IsBuild property.

C#
RadListView1.FilterExpressions
.BuildExpression(expression => expression
.GreaterThan("ShippedDate", DateTime.Parse("7/10/1996"))
.Or().EqualTo("OrderID", 42)
.And().Group(group =>
group.IsNotEmpty("ShipCountry")
.And().Contains("ShipCountry", "G")
)
);

RadListView filter expression enumerators

RadListView supplies the following enumerators to help you build the desired filter expressions:

EnumeratorValues
RadListViewFilterFunctionContains
EqualTo
GreaterThan
GreaterThanOrEqualTo
IsEmpty
IsNull
LessThan
LessThanOrEqualTo
NotEqualTo
NotIsEmpty
NotIsNull
RadListViewGroupFilterOperatorAnd
Or

For a live example on how RadListView built-in filtering capabilities can be used, review the online demo here .