Skip to main content

DxGrid.SetFilterCriteria(CriteriaOperator) Method

Applies a filter to grid data.

Namespace: DevExpress.Blazor

Assembly: DevExpress.Blazor.v24.2.dll

NuGet Package: DevExpress.Blazor

Declaration

public void SetFilterCriteria(
    CriteriaOperator criteria
)

Parameters

Name Type Description
criteria CriteriaOperator

An object that specifies the filter expression.

Remarks

Call the SetFilterCriteria method to filter grid data in code.

To apply a filter, create a criteria operator object that specifies the filter expression and pass this object to the SetFilterCriteria method. When you call this method, the Grid clears any previously applied filters. You can filter grid data by every data source field, including the fields that are not displayed in the grid.

When a filter is applied, the Grid raises the FilterCriteriaChanged event.

<DxTagBox Data="Categories"
          Values="SelectedCategories"
          ValuesChanged="(IEnumerable<Category> values) => TagBox_ValuesChanged(values)"
          TextFieldName="CategoryName"/>

<DxGrid @ref="Grid" Data="Products" CustomizeCellDisplayText="Grid_CustomizeCellDisplayText">
    <Columns>
        <DxGridDataColumn FieldName="ProductName" MinWidth="100" />
        <DxGridDataColumn FieldName="CategoryId" SortMode="GridColumnSortMode.DisplayText" 
                          Caption="Category" MinWidth="100" TextAlignment="GridTextAlignment.Left" />
        <DxGridDataColumn FieldName="UnitPrice" DisplayFormat="c" Width="10%" />
        <DxGridDataColumn FieldName="UnitsInStock" Width="10%" />
        <DxGridDataColumn FieldName="QuantityPerUnit" Width="15%" MinWidth="80" />
    </Columns>
</DxGrid>

@code {
    IGrid Grid { get; set; }
    IEnumerable<Product> Products { get; set; }
    IEnumerable<Category> Categories { get; set; }
    IEnumerable<Category> SelectedCategories { get; set; }
    protected override async Task OnInitializedAsync() {
        Categories = await NwindDataService.GetCategoriesAsync();
        Products = await NwindDataService.GetProductsAsync();
    }
    void TagBox_ValuesChanged(IEnumerable<Category> newSelectedCategories) {
        SelectedCategories = newSelectedCategories;
        var filterCriteria = SelectedCategories.Count() > 0
            ? new InOperator("CategoryId", SelectedCategories.Select(c => c.CategoryId))
            : null;
        Grid.SetFilterCriteria(filterCriteria);
    }
    void Grid_CustomizeCellDisplayText(GridCustomizeCellDisplayTextEventArgs e) {
        if(e.FieldName == "CategoryId") {
            e.DisplayText = Categories.Where(c => c.CategoryId == (int)e.Value).First().CategoryName;
        }
    }
}

You can apply a filter to a column without resetting the existing filter. Pass a criteria operator object to the SetFieldFilterCriteria(String, CriteriaOperator) method to apply the filter to a particular data field. This method uses the AND operator to combine filters for different fields.

GridDevExtremeDataSource Specifics

The GridDevExtremeDataSource is based on the DevExtreme.AspNet.Data library that uses its own filter language syntax. Some structures of criteria language syntax cannot be converted to DevExtreme data source syntax. This section describes criteria operators that are supported by the GridDevExtremeDataSource.

Binary Operator

The BinaryOperator criteria operator allows you to compare operand property with operand value. You can use the following operator types for comparison: Equal, NotEqual, Greater, GreaterOrEqual, Less, and LessOrEqual.

var criteria = new BinaryOperator("UnitPrice", 20, BinaryOperatorType.Less);

Unary Operator

The UnaryOperator criteria operator allows you to apply Not or IsNull operation to an expression.

var criteria = new UnaryOperator(UnaryOperatorType.Not, new UnaryOperator(UnaryOperatorType.IsNull, "Region"));

Function Operator

The FunctionOperator criteria operator allows you to create a complex filter criteria with the following operator types:

  • IsNull and IsNullOrEmpty operands indicate whether a specified operand is a null reference or an empty string.
  • InRange operand determines whether a property value is contained in the specified range.
  • StartsWith, EndsWith, and Contains operands allow you to compare a property with a string value.
var criteria = new FunctionOperator(FunctionOperatorType.StartsWith, new OperandProperty("ShipCountry"), "it");

Group Operator

The GroupOperator criteria operator allows you to create a logical expression that groups two or more operands with a logical AND or OR.

var criteria = new GroupOperator(GroupOperatorType.And,
        new BinaryOperator("UnitPrice", 20, BinaryOperatorType.Less),
        new FunctionOperator(FunctionOperatorType.StartsWith, new OperandProperty("ShipCountry"), "it"));

Between Operator

The BetweenOperator criteria operator allows you to determine whether a criteria expression lies between the specified range of values.

var criteria = new BetweenOperator("UnitPrice", 15, 20);

In Operator

The InOperator criteria operator allows you to determine whether a property value matches any value in a specified list.

var criteria = new InOperator("ShipCountry", new string[] { "Italy", "German", "France" });

Run Demo: Grid - Filter API

For more information, see the following topic: Filter API in Blazor Grid.

See Also