New to Telerik UI for .NET MAUI? Start a free 30-day trial
Using
Remembering and Restoring Expanded Rows in TreeDataGrid for .NET MAUI
Updated on Sep 26, 2025
Environment
| Version | Product | Author |
|---|---|---|
| 11.1.0 | Telerik UI for .NET MAUI TreeDataGrid | Dobrinka Yordanova |
Description
I want to remember the expanded rows in the TreeDataGrid and restore them later. What is the most efficient way to achieve this?
This knowledge base article also answers the following questions:
- How can I store expanded items in TreeDataGrid for later use?
- How can I restore the expansion state in TreeDataGrid after changes?
- What is the best way to manage expanded rows in TreeDataGrid?
Solution
To achieve the scenario you can use one of the following approaches:
Using Expand and Collapse Methods
To achieve this, follow these steps:
- Use
Expand()andCollapse()methods to expand or collapse items. - Use the
IsExpandedmethod to check whether an item is expanded. - Store the expanded items in a collection, such as a
List<YourDataClass>. - Iterate through the collection to restore the expanded state by calling the
Expand()method.
C#
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
this.BindingContext = new ViewModel();
}
// Collection for storing expanded items
List<Data> expandedItems = new List<Data>();
private void Button_Clicked(object sender, EventArgs e)
{
var items = (IEnumerable)this.tdg.ItemsSource;
this.expandedItems = new List<Data>();
AddExpandedItems(items, expandedItems);
}
private void AddExpandedItems(IEnumerable items, List<Data> expandedItems)
{
foreach (Data item in items)
{
if (this.tdg.IsExpanded(item))
{
expandedItems.Add(item);
}
IEnumerable childrenOfTheItem = item.Children;
AddExpandedItems(childrenOfTheItem, expandedItems);
}
}
private void Button2_Clicked(object sender, EventArgs e)
{
foreach (Data item in this.expandedItems)
{
this.tdg.Expand(item);
}
}
}
Using IsExpandableBinding
As an alternative, use the IsExpandableBinding property at the descriptor level. This approach provides direct control over the expandability of rows via data binding.
- Add a
boolproperty in the data model to indicate whether an item is expandable. - Bind this property to the
IsExpandableBindingproperty of the TreeDataGrid descriptor. - Implement the logic in button clicks to get and restore expanded items.