Programmatically execute grid filters

Hello,

I want to calculate the number of products that satisfy the filters for a grid whose state is stored in the database. I wrote a scheduled job that for each such grid it first initializes it - var grid = new SfGrid<Productc>() and afterwards sets the state that was stored in the in the database - await grid.SetPersistDataAsync(state). However, this lead to a null exception. Is there a way to do this without rendering the grid first? Alternatively could you provide me with a function that takes in all the products and the filter state of the current grid and returns the products that satisfy the filtering criteria?

Krasimir


5 Replies 1 reply marked as answer

SP Sarveswaran Palani Syncfusion Team September 22, 2023 04:13 AM UTC

Hi Krasimir,

Greetings from Syncfusion support.

From your previous query, it seems that you have encountered a null exception error when using the SetPersistDataAsync method. It's important to note that this method should be used after initializing the Grid component. If you attempt to use it before initializing the Grid, it can result in a null exception error, which appears to be the issue you're facing.

Regarding your need to retrieve specific filters from a database, we recommend using the query property to address this requirement and avoid the reported issue. You can find more details on how to implement this in our related documentation. If we have misunderstood your query or if you require further assistance, please provide more details about your requirements along with a sample and code snippet. With this additional information, we will be able to thoroughly assess the issue and provide a precise solution as soon as possible.

Reference: https://blazor.syncfusion.com/documentation/datagrid/data-binding#change-query-parameter-value-dynamically

Regards,
Sarvesh



KI Krasimir Ivanov September 24, 2023 02:53 PM UTC

Yes, this is the issue I am facing. However, the proposed solution does not work for me because I need to create a Grid object without initializing it inside a razor component. Everything needs to be done inside a function - creating the grid object, setting the filters and binding the data. The reason why I cannot use razor components is because I need to create a scheduled job that executes this function periodically for each of the stored grids.



SP Sarveswaran Palani Syncfusion Team September 27, 2023 02:41 PM UTC

Hi Krasimir,

Based on your last query, it appears that you are attempting to initialize the grid component from a method rather than using a Razor page. Additionally, you are trying to save persistent state using the SetPersistDataAsync method before the grid initialization. We would like to clarify that it's not possible to save the state before grid initialization; the state can only be saved after the grid has been initialized. If there are specific details or requirements related to your scenario that we may have misunderstood, kindly provide more information along with your grid code snippet and a sample to assist you further.

Regards,
Sarvesh



KI Krasimir Ivanov November 4, 2023 05:14 PM UTC

Hi,

Yes, this is what I want - to initialize the grid component from a method. I need to be able to this to calculate the number of data points that satisfy the filters in the stored grids. This needs to be calculated for every stored grid in a scheduled job. If initializing the grid is not possible can you provide some other workaround to achieve this? How can I use the stored grid state and all data points to calculate the number of data points that satisfy the filters in the stored grid state?



SP Sarveswaran Palani Syncfusion Team November 8, 2023 04:51 PM UTC

Hi Krasimir,

Based on your query, it seems you would like to initialize the grid component from a method. To achieve this, we have provided a sample that demonstrates how to initialize the grid component within a method. You can refer to the attached sample and code snippet for more details and implementation.

<SfGrid DataSource="@gridData" AllowPaging="true">

    <GridColumns>

        <GridColumn Field="Id" HeaderText="ID" Width="100" />

        <GridColumn Field="Name" HeaderText="Name" Width="150" />

        <GridColumn Field="Age" HeaderText="Age" Width="100" />

    </GridColumns>

</SfGrid>

 

@code {

    public List<MyData> gridData { get; set; }

 

    protected override void OnInitialized()

    {

        // Call a method to prepare data

        gridData = PrepareGridData();

    }

 

    public List<MyData> PrepareGridData()

    {

        var data = new List<MyData>

        {

            new MyData { Id = 1, Name = "John", Age = 30 },

            new MyData { Id = 2, Name = "Alice", Age = 25 },

            new MyData { Id = 3, Name = "Bob", Age = 35 },

        };

 

        return data;

    }

}

 



Sample: https://blazorplayground.syncfusion.com/embed/htrqsijbfBAEGGsH?appbar=true&editor=true&result=true&errorlist=true&theme=bootstrap5

If you have any further queries, please get back to us.

Regards,
Sarvesh


Marked as answer
Loader.
Up arrow icon