Back to Devexpress

Filter Grid View Data in Code

aspnet-3752-components-grid-view-concepts-filter-data-filtering-in-code.md

latest3.3 KB
Original Source

Filter Grid View Data in Code

  • Jul 15, 2021
  • 2 minutes to read

To apply a filter to ASPxGridView , you can do one of the following.

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:

csharp
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;
vb
Private Sub SurroundingSub()
    ASPxGridView1.FilterExpression = "[Budget] > 100000 AND [Location] = 'Monterey'"
    ASPxGridView1.SettingsText.Title = ASPxGridView1.FilterExpression
    Dim date1 As DateTime = New DateTime(1995, 1, 1)
    Dim date2 As DateTime = New DateTime(1995, 7, 1)
    Dim filter As String = String.Format("[OrderDate] > #{0}# And [OrderDate] < #{1}#", date1.ToShortDateString(), date2.ToShortDateString())
    ASPxGridView1.FilterExpression = filter
End Sub

To clear filter settings for a single column, pass an empty string to the column’s GridViewDataColumn.AutoFilterBy method.

csharp
((GridViewDataColumn)ASPxGridView2.Columns["Department"]).AutoFilterBy(String.Empty);
vb
(CType(ASPxGridView2.Columns("Department"), GridViewDataColumn)).AutoFilterBy(String.Empty)

Setting the ASPxGridBase.FilterExpression property to an empty string clears the filter expression applied to ASPxGridView.

csharp
ASPxGridView2.FilterExpression = String.Empty;
vb
ASPxGridView2.FilterExpression = String.Empty

See Also

Criteria Language Syntax