This is a migrated thread and some comments may be shown as answers.

ObservableCollection as data source

1 Answer 521 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Robert
Top achievements
Rank 1
Iron
Veteran
Robert asked on 25 Mar 2021, 01:23 PM

The following code leads to an infinite loop and call stack exhaustion.
How to use the grid with an ObservableCollection as data source?

@page "/Test"
@using System.Collections.ObjectModel
 
<TelerikGrid Data="_data" Pageable="true" OnRead="@Read"/>
 
@code {
    private readonly ObservableCollection<object> _data = new();
 
    protected Task Read(GridReadEventArgs args)
    {
        Console.WriteLine("Read");
        _data.Add(new());
        return(Task.FromResult(0));
    }
}

1 Answer, 1 is accepted

Sort by
0
Marin Bratanov
Telerik team
answered on 29 Mar 2021, 06:44 AM

Hello Robert,

This is documented in the article about the OnRead event:

If you are using an ObservableCollection, make sure to create a new one, because using .Add(), .Remove() or .Clear() on it will cause an infinite loop - the grid monitors the ObservableCollection events and updates its data, which will fire OnRead.

I've added a note about it in the article on handling Observable data too (commit link that will be live with our next upload).

 

Regards,
Marin Bratanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Grid
Asked by
Robert
Top achievements
Rank 1
Iron
Veteran
Answers by
Marin Bratanov
Telerik team
Share this question
or