New to Telerik UI for Blazor? Download free 30-day trial

TreeList - Refresh Data

The most common reason you would use an ObservableCollection is to make a component (like a grid, treeview, treelist, dropdown) change or react when you change that collection.

When you want to refresh the component data source like that, there are two important framework behaviors you need to be aware of - when ObservableCollection instances fire events, and how to refresh the data of a component when it is not an observable collection.

In this article:

Rebind Method

You can refresh the data by invoking the Rebind method. Use the component reference to call the Rebind method after you have made the data changes (for example adding, removing items). This is needed in case you are not using Observable data or resetting the collection reference. Calling Rebind will force the component process the available data anew to reflect the updates.

Use the Rebind method to refresh the data.

@* Add/remove item and rebind the Treelist to react to that change. *@

<TelerikButton OnClick="@AddRootItem">Add root item at the end of the collection</TelerikButton>

<TelerikButton OnClick="@RemoveItem">Remove last item</TelerikButton>

<TelerikTreeList @ref="@TreeListRef"
                 Data="@TreeListData"
                 IdField="EmployeeId"
                 ParentIdField="ReportsTo"
                 Pageable="true">
    <TreeListColumns>
        <TreeListColumn Field="FirstName" Expandable="true" />
        <TreeListColumn Field="EmployeeId" />
    </TreeListColumns>
</TelerikTreeList>

@code {
    private TelerikTreeList<Employee> TreeListRef;

    private List<Employee> TreeListData { get; set; }

    private void AddRootItem()
    {
        var i = TreeListData.Count + 1;

        TreeListData.Add(new Employee()
        {
            EmployeeId = i,
            ReportsTo = null,
            FirstName = "Employee  " + i.ToString()
        });

        TreeListRef.Rebind();
    }

    private void RemoveItem()
    {
        if (TreeListData.Count > 0)
        {
            TreeListData.RemoveAt(TreeListData.IndexOf(TreeListData.Last()));
        }

        TreeListRef.Rebind();
    }

    public class Employee
    {
        public int EmployeeId { get; set; }
        public string FirstName { get; set; }
        public int? ReportsTo { get; set; }
    }

    protected override void OnInitialized()
    {
        TreeListData = new List<Employee>();
        var rand = new Random();
        int currentId = 1;

        for (int i = 1; i < 6; i++)
        {
            TreeListData.Add(new Employee()
            {
                EmployeeId = currentId,
                ReportsTo = null,
                FirstName = "Employee  " + i.ToString()
            });

            currentId++;
        }
        for (int i = 1; i < 6; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                TreeListData.Add(new Employee()
                    {
                        EmployeeId = currentId,
                        ReportsTo = i,
                        FirstName = "    Employee " + i + " : " + j.ToString()
                    });

                currentId++;
            }
        }
    }
}

Observable Data

Databound components can benefit from live data - when the data source collection changes, the components should update to reflect that change. Most data-bound components in the Telerik UI for Blazor suite implement such functionality.

When the Data of the component is a collection that implements the INotifyCollectionChanged interface (such as ObservableCollection), the Telerik components subscribe to its CollectionChanged event to update. This means that adding items, removing items, or clearing the collection updates the components (its .Add(), .Remove() and .Clear() methods).

The Observable collections fire the CollectionChanged event only when their Add, Remove and Clear methods are called. They do not fire it when you change the value of a field of one of their elements.

Bind the TreeList to an ObservableCollection, so it can react to collection changes.

@* Add/remove item to see how the Treelist reacts to that change. *@

@using System.Collections.ObjectModel

<TelerikButton OnClick="@AddRootItem">Add root item at the end of the collection</TelerikButton>

<TelerikButton OnClick="@RemoveItem">Remove last item</TelerikButton>

<TelerikTreeList Data="@TreeListData"
                 IdField="EmployeeId"
                 ParentIdField="ReportsTo"
                 Pageable="true">
    <TreeListColumns>
        <TreeListColumn Field="FirstName" Expandable="true"/>
        <TreeListColumn Field="EmployeeId"/>
    </TreeListColumns>
</TelerikTreeList>

@code {
    private ObservableCollection<Employee> TreeListData { get; set; }

    private void AddRootItem()
    {
        var i = TreeListData.Count + 1;
        TreeListData.Add(new Employee()
        {
            EmployeeId = i,
            ReportsTo = null,
            FirstName = "Employee  " + i.ToString()
        });
    }

    private void RemoveItem()
    {
        if (TreeListData.Count > 0)
        {
            TreeListData.RemoveAt(TreeListData.IndexOf(TreeListData.Last()));
        }
    }

    public class Employee
    {
        public int EmployeeId { get; set; }
        public string FirstName { get; set; }
        public int? ReportsTo { get; set; }
    }

    protected override void OnInitialized()
    {
        TreeListData = new ObservableCollection<Employee>();
        var rand = new Random();
        int currentId = 1;

        for (int i = 1; i < 6; i++)
        {
            TreeListData.Add(new Employee()
            {
                EmployeeId = currentId,
                ReportsTo = null,
                FirstName = "Employee  " + i.ToString()
            });

            currentId++;
        }
        for (int i = 1; i < 6; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                TreeListData.Add(new Employee()
                {
                    EmployeeId = currentId,
                    ReportsTo = i,
                    FirstName = "    Employee " + i + " : " + j.ToString()
                });

                currentId++;
            }
        }
    }
}

If you need to add/remove many items to/from the collection, consider creating a new collection and provide its reference to the data parameter. Thus, the component will re-render only once (when the data collection reference is changed) instead of re-rendering multiple times in response to the Add/Remove events.

New Collection Reference

In Blazor, the framework will fire the OnParametersSet event of a child component (which is how child components can react to outside changes) only when it can detect a change in the object it receives through the corresponding parameter (like Data for the data sources of Telerik components). This detection works as follows:

  • For primitive types (such as numbers, strings), this happens when their value changes.

  • For complex types (such as data collections like List, or any IEnumerable, and application-specific models/objects), this happens when the object reference changes.

    Thus, you would usually need to create a new reference for the view-model field (such as TreeViewData = new List<MyTreeViewItem>(theUpdatedDataCollection);) when you want the component to update.

Create new collection reference to refresh the TreeList data.

@* Add/remove item or change the collection to see how the Treelist reacts to that change. *@

<TelerikButton OnClick="@AddRootItem">Add root item at the end of the collection</TelerikButton>

<TelerikButton OnClick="@RemoveItem">Remove last item</TelerikButton>

<TelerikButton OnClick="@ClearData">Clear Data</TelerikButton>

<TelerikButton OnClick="@LoadNewData">Load new data</TelerikButton>

<TelerikTreeList Data="@TreeListData"
                 IdField="EmployeeId"
                 ParentIdField="ReportsTo"
                 Pageable="true">
    <TreeListColumns>
        <TreeListColumn Field="FirstName" Expandable="true"/>
        <TreeListColumn Field="EmployeeId"/>
    </TreeListColumns>
</TelerikTreeList>

@code {
    private List<Employee> TreeListData { get; set; }

    private void AddRootItem()
    {
        var i = TreeListData.Count + 10;
        TreeListData.Add(new Employee()
        {
            EmployeeId = i,
            ReportsTo = null,
            FirstName = "Employee  " + i.ToString()
        });
        TreeListData = new List<Employee>(TreeListData);
    }

    private void RemoveItem()
    {
        if (TreeListData.Count > 0)
        {
            TreeListData.RemoveAt(TreeListData.IndexOf(TreeListData.Last()));
            TreeListData = new List<Employee>(TreeListData);
        }
    }

    private void ClearData()
    {
        TreeListData.Clear();
        TreeListData = new List<Employee>(TreeListData);
    }

    private void LoadNewData()
    {
        var newData = new List<Employee>();
        var rand = new Random();
        int currentId = 6;

        for (int i = 6; i < 11; i++)
        {
            newData.Add(new Employee()
            {
                EmployeeId = currentId,
                ReportsTo = null,
                FirstName = "Employee  " + i.ToString()
            });

            currentId++;
        }
        for (int i = 6; i < 11; i++)
        {
            for (int j = 12; j < 16; j++)
            {
                newData.Add(new Employee()
                {
                    EmployeeId = currentId,
                    ReportsTo = i,
                    FirstName = "    Employee " + i + " : " + j.ToString()
                });

                currentId++;
            }
        }

        TreeListData = new List<Employee>(newData);

        Console.WriteLine("New data collection loaded.");
    }

    public class Employee
    {
        public int EmployeeId { get; set; }
        public string FirstName { get; set; }
        public int? ReportsTo { get; set; }
    }

    protected override void OnInitialized()
    {
        TreeListData = new List<Employee>();
        var rand = new Random();
        int currentId = 1;

        for (int i = 1; i < 6; i++)
        {
            TreeListData.Add(new Employee()
            {
                EmployeeId = currentId,
                ReportsTo = null,
                FirstName = "Employee  " + i.ToString()
            });

            currentId++;
        }
        for (int i = 1; i < 6; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                TreeListData.Add(new Employee()
                {
                    EmployeeId = currentId,
                    ReportsTo = i,
                    FirstName = "    Employee " + i + " : " + j.ToString()
                });

                currentId++;
            }
        }
    }
}

See Also

In this article