Grid Filter Expression definition

Hey guys, 
we use your Grid and try to give our customers some more flexibilty in filtering and build a own filter creation tool. 

We want to have the filter expression:
"Name contains John AND Birthdate greaterthan 01.01.1992 OR Birthdate smallerthan 31.12.2000"

filtered as:
"Name contains John AND (Birthdate greaterthan 01.01.1992 OR Birthdate smallerthan 31.12.2000)"

If we use "Grid.FilterSettings.UpdateChildProperties("Columns", filter);" we don't get what we need because of the missing brackets. 

How do we do a filtering like this? Can we apply multiple conditions with different predicates in Grid.FilterByColumn() or something like that? 

Best regards

1 Reply 1 reply marked as answer

RN Rahul Narayanasamy Syncfusion Team March 30, 2021 12:34 PM UTC

Hi Patrick, 

Greetings from Syncfusion. 

Query: Grid Filter Expression definition - filtered as: "Name contains John AND (Birthdate greaterthan 01.01.1992 OR Birthdate smallerthan 31.12.2000)" 

We have validated your query and you want to build the filter predicate and perform filtering operation in Grid. You can generate and pass the predicates in Grid Query property to achieve your requirement. Find the below code snippets for your reference. 

In the below code example, we have filtered the CustomerID value ALFKI, OrderDate between DateTime(2010, 5, 2) and DateTime(2010, 5, 7). Based on your case, you can change the And/Or predicate to achieve your requirement. 

 
@using Syncfusion.Blazor.Data 
@using Syncfusion.Blazor.Grids 
 
<button @onclick="Filter"> Filtering</button> 
<button @onclick="ClearFilter"> Clear Filtering</button> 
 
<SfGrid @ref="Grid" Query="GridQuery" DataSource="@Orders" AllowFiltering="true"> 
    <GridEvents OnActionBegin="ActionBeginHandler" TValue="Order"></GridEvents> 
    <GridColumns> 
        <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" TextAlign="TextAlign.Right" Width="120"> 
        </GridColumn> 
        <GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" Width="150"> 
        </GridColumn> 
        <GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" Format="C2" TextAlign="TextAlign.Right" Width="120"> 
        </GridColumn> 
        <GridColumn Field="OrderDate" HeaderText=" Order Date" Format="d" Type="ColumnType.Date" TextAlign="TextAlign.Right" Width="130"></GridColumn> 
    </GridColumns> 
</SfGrid> 
 
@code{ 
    public List<Order> Orders { get; set; } 
    public string val = "ANANTR"; 
    SfGrid<Order> Grid { get; set; } 
    public Query GridQuery { get; set; } 
 
    protected override void OnInitialized() 
    { 
        Orders = Enumerable.Range(1, 75).Select(x => new Order() 
        { 
            OrderID = 1000 + x, 
            CustomerID = (new string[] { "ALFKI", "ANANTR", "ANTON", "BLONP", "BOLID" })[new Random().Next(5)], 
            Freight = (new double?[] { 2.3, 3.3, 4.3, 5.3, 6.3 })[new Random().Next(5)] , 
            OrderDate = (new DateTime[] { new DateTime(2010, 5, 1), new DateTime(2010, 5, 2), new DateTime(2010, 5, 3), new DateTime(2010, 5, 7), new DateTime(2010, 5, 8) })[new Random().Next(4)] 
        }).ToList(); 
    } 
 
    public class Order 
    { 
        public int? OrderID { get; set; } 
        public string CustomerID { get; set; } 
        public DateTime? OrderDate { get; set; } 
        public double? Freight { get; set; } 
    } 
    public void ActionBeginHandler(ActionEventArgs<Order> args) 
    { 
        // Here you can customize your code 
    } 
 
    public void Filter() 
    { 
        GridQuery = new Query(); 
        var ColPre = new WhereFilter(); 
        var last = new WhereFilter(); 
        List<WhereFilter> OrPredicate = new List<WhereFilter>(); 
        List<WhereFilter> AndPredicate = new List<WhereFilter>(); 
        OrPredicate.Add(new WhereFilter() 
        { 
            Field = "OrderDate", 
            value = new DateTime(2010, 5, 2), 
            Operator = "greaterthanorequal", 
            Condition = "or" 
        }); 
        AndPredicate.Add(new WhereFilter() 
        { 
            Field = "CustomerID", 
            value = "ALFKI", 
            Operator = "contains", 
            IgnoreCase = true 
        }); 
        OrPredicate.Add(new WhereFilter() 
        { 
            Field = "OrderDate", 
            value = new DateTime(2010, 5, 7), 
            Operator = "lessthanorequal", 
            Condition = "or" 
        }); 
        ColPre = WhereFilter.And(AndPredicate); 
        last = WhereFilter.And(OrPredicate); 
        GridQuery = new Query().Where(ColPre.And(last)); 
    } 
    public void ClearFilter() 
    { 
        GridQuery = new Query(); 
    } 
} 

Please let us know if you have any concerns. 

Regards, 
Rahul 
 


Marked as answer
Loader.
Up arrow icon