Is it possible to add a query from both QueryBuilder and DateRangePicker for a SfGrid? Also, can I filter a Grid using Querybuilder.SetRulesFromSql?

I have attached a sample and I am trying to add the same query from both QueryBuilder and DateRangePicker for changing values in a Grid. 

  1. Is there a way to check what wherefilter is applied and possibly remove them when I either remove the QueryBuilder or clear the DateRangePicker?
  2. I am also trying to add Query to the Grid using SetRulesFromSql but it isn't filtering the Grid even though it fills the QueryBuilder.

Attachment: QueryBuilderGridOData681435691_e280d8e4.zip

1 Reply

YA YuvanShankar Arunagiri Syncfusion Team May 30, 2022 06:53 AM UTC

Hi Nick,


Query: Is there a way to check what wherefilter is applied and possibly remove them when I either remove the QueryBuilder or clear the DateRangePicker?

Already, you defined separate trigger event for query builder and daterangepicker and you easily differentiate them using event. If you remove the querybuilder or dateRangePicker it will remove the old predicate and loaded the original query.

if (predicate != null)

      {

        GridQueryData = new Query();

        GridQueryData = GridQueryData.Where(predicate);

      }

      else

      {

        GridQueryData = new Query();

      }

Query: I am also trying to add Query to the Grid using SetRulesFromSql but it isn't filtering the Grid even though it fills the QueryBuilder.

We have checked your sample and modify it based on your requirement. Please refer the below code snippet.

[Index.razor]:

@if (isQueryLoaded)

{

  <div class="col-lg-12 control-section" style="padding: 5px;">

    <div class="content-wrapper">

      <div class="row">

        <SfGrid TValue="Order" AllowPaging="true" Query="@GridQueryData">

          <SfDataManager Url="https://services.odata.org/V4/Northwind/Northwind.svc/Orders/" Adaptor="Adaptors.ODataV4Adaptor"></SfDataManager>

          <GridColumns>

            <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="TextAlign.Right" Width="120"></GridColumn>

            <GridColumn Field=@nameof(Order.CustomerID) HeaderText="Customer Name" Width="150"></GridColumn>

            <GridColumn Field=@nameof(Order.ShipCity) HeaderText=" Ship City" Width="130"></GridColumn>

            <GridColumn Field=@nameof(Order.ShipCountry) HeaderText="Ship Country" Width="120"></GridColumn>

            <GridColumn Field=@nameof(Order.OrderDate) HeaderText="Order Date" Width="120"></GridColumn>

          </GridColumns>

        </SfGrid>

      </div>

    </div>

  </div>

}

 

@code{

 

    SfQueryBuilder<Order> Querybuilder;

    private Query GridQueryData = new Query();

    SfDateRangePicker<DateTime?> DatePicker { get; set; }

    private bool isQueryLoaded = false;

 

    private void Created()

    {

      Querybuilder.SetRulesFromSql("ShipCountry = 'France'");

      UpdateRule(null);

      isQueryLoaded = true;

    }

 

    private void UpdateRule(RuleChangeEventArgs args)

    {

      var predicate = Querybuilder.GetPredicate();

      if (predicate != null)

      {

        GridQueryData = new Query();

        GridQueryData = GridQueryData.Where(predicate);

      }

      else

      {

        GridQueryData = new Query();

      }

    }

 


For your convenience, we have attached the sample.


Please get back to us if you need further assistance on this.


Regards,

Yuvan Shankar A


Attachment: QueryBuilderGridOData681435691_aa9f5c85.zip

Loader.
Up arrow icon