Filter Grid View Data in Code
- 2 minutes to read
To apply a filter to ASPxGridView, you can do one of the following.
- Use the ASPxGridView.AutoFilterByColumn method.
- Specify the filter expression and assign it to the ASPxGridBase.FilterExpression.
- Use the column’s GridViewDataColumn.AutoFilterBy method.
The ASPxGridBase.FilterExpression property allows you to provide complex filter criteria (filter data by multiple columns).
To filter data on the client, use the ASPxClientGridView.AutoFilterByColumn method.
Example 1
The following code applies a filter that selects departments whose budget is greater than 100,000 and that are located in Monterey. The filter criteria are displayed within the ASPxGridView’s Title Panel.
The image below shows the result:
ASPxGridView1.FilterExpression = "[Budget] > 100000 AND [Location] = 'Monterey'";
ASPxGridView1.SettingsText.Title = ASPxGridView1.FilterExpression;
// ...
// An example on how to filter by dates
DateTime date1 = new DateTime(1995, 1, 1);
DateTime date2 = new DateTime(1995, 7, 1);
string filter = String.Format("[OrderDate] > #{0}# And [OrderDate] < #{1}#",
date1.ToShortDateString(), date2.ToShortDateString());
ASPxGridView1.FilterExpression = filter;
To clear filter settings for a single column, pass an empty string to the column’s GridViewDataColumn.AutoFilterBy method.
Setting the ASPxGridBase.FilterExpression property to an empty string clears the filter expression applied to ASPxGridView.