Refresh grid filter from code

2 Answers 22 Views
Grid
Claudio
Top achievements
Rank 2
Bronze
Bronze
Iron
Claudio asked on 28 Jan 2025, 11:32 AM

I have a paged TelerikGrid loaded using OnRead event with 2 filter modes: Inline and programmately.

With inline filter i manage a FilterCellTemplate and call FilterCellTemplateContext.FilterAsync() to refresh the grid after setting the filter value.

If i set a filter programmately, to update the grid i call Grid.Rebind(), but i need to set the grid to page 1, so i need to set the Page property binding to 1 before calling Grid.Rebind.

There is a better way to refresh the grid for filtering programmately? the best would be a method to call (as done with FilterCellTemplateContext.FilterAsync()) who manage the page reset.

Thanks

2 Answers, 1 is accepted

Sort by
0
Tsvetomir
Telerik team
answered on 31 Jan 2025, 08:02 AM

Hi Claudio,

To achieve programmatic filtering and reset the Grid to the first page efficiently, the approach you are currently using is indeed one of the best available options. However, you can use the following to achieve your goal:

  1. Set Filter Descriptors -  Programmatically update the filter descriptors as needed.

  2. Reset the Page -  Before rebinding, configure the Grid state by setting its page to 1.

  3. Rebind the Grid -  Use the Grid.Rebind() method to refresh the grid data.

Here's how you can implement this:

@code {
    TelerikGrid<MyDataType> gridRef;
    GridState<MyDataType> gridState = new GridState<MyDataType>();

    private void ApplyProgrammaticFilter()
    {
        // Set your filter descriptors here
        gridState.FilterDescriptors = new List<FilterDescriptorBase>
        {
            new FilterDescriptor("MyColumn", FilterOperator.Contains, "FilterValue")
        };

        // Reset to the first page
        gridState.Page = 1;

        // Apply the state and rebind the grid
        gridRef.SetState(gridState);
        gridRef.Rebind();
    }
}

This method manages the grid state directly, including filters and pagination. By setting the Page property to "1", you ensure that the grid starts from the first page after applying the filters. In addition, you can refer to our Grid - Refresh Data article to explore the other available options for refreshing the Grid.

I hope the provided information serves you well in continuing with your project.

    Regards,
    Tsvetomir
    Progress Telerik

    Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

    0
    Claudio
    Top achievements
    Rank 2
    Bronze
    Bronze
    Iron
    answered on 31 Jan 2025, 08:48 AM | edited on 31 Jan 2025, 08:49 AM

    Thanks Tsvetomit, i made an extension method:

    public static async Task RebindAsync<T>(this Telerik.Blazor.Components.TelerikGrid<T> grid, bool resetToFirstPage = false)
    {
        if (!resetToFirstPage)
        {
            grid.Rebind();
            return;
        }
    
        var gridState = grid.GetState();
        gridState.Page = 1;
    
        await grid.SetStateAsync(gridState);    
    }

    Tags
    Grid
    Asked by
    Claudio
    Top achievements
    Rank 2
    Bronze
    Bronze
    Iron
    Answers by
    Tsvetomir
    Telerik team
    Claudio
    Top achievements
    Rank 2
    Bronze
    Bronze
    Iron
    Share this question
    or