Skip to content

Commit a502202

Browse files
author
KB Bot
committed
Added new kb article remembering-restoring-expanded-rows-treedatagrid-dotnet-maui
1 parent cb12a64 commit a502202

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
---
2+
title: Remembering and Restoring Expanded Rows in TreeDataGrid for .NET MAUI
3+
description: Learn how to efficiently remember expanded rows and restore them in TreeDataGrid for .NET MAUI.
4+
type: how-to
5+
page_title: Efficiently Remember and Restore Expanded Rows in TreeDataGrid for .NET MAUI
6+
meta_title: Efficiently Remember and Restore Expanded Rows in TreeDataGrid for .NET MAUI
7+
slug: remembering-restoring-expanded-rows-treedatagrid-dotnet-maui
8+
tags: treedatagrid, .net maui, expand, collapse, isexpanded, isexpandablebinding
9+
res_type: kb
10+
---
11+
12+
## Environment
13+
14+
| Version | Product | Author |
15+
| --- | --- | ---- |
16+
| 11.1.0 | Telerik UI for .NET MAUI TreeDataGrid | [Dobrinka Yordanova](https://www.telerik.com/blogs/author/dobrinka-yordanova) |
17+
18+
## Description
19+
20+
I want to remember the expanded rows in the TreeDataGrid and restore them later. What is the most efficient way to achieve this?
21+
22+
This knowledge base article also answers the following questions:
23+
- How can I store expanded items in TreeDataGrid for later use?
24+
- How can I restore the expansion state in TreeDataGrid after changes?
25+
- What is the best way to manage expanded rows in TreeDataGrid?
26+
27+
## Solution
28+
29+
To achieve the scenario you can use one of the following approaches:
30+
* [Using `Expand` and `Collapse` Methods](#using-expand-and-collapse-methods)
31+
* [Using `IsExpandableBinding` property of the `TreeDataGridDescriptor`](#using-isexpandablebinding)
32+
33+
### Using Expand and Collapse Methods
34+
35+
To achieve this, follow these steps:
36+
37+
1. Use `Expand()` and `Collapse()` methods to expand or collapse items.
38+
2. Use the `IsExpanded` method to check whether an item is expanded.
39+
3. Store the expanded items in a collection, such as a `List<YourDataClass>`.
40+
4. Iterate through the collection to restore the expanded state by calling the `Expand()` method.
41+
42+
```C#
43+
public partial class MainPage : ContentPage
44+
{
45+
public MainPage()
46+
{
47+
InitializeComponent();
48+
this.BindingContext = new ViewModel();
49+
}
50+
51+
// Collection for storing expanded items
52+
List<Data> expandedItems = new List<Data>();
53+
54+
private void Button_Clicked(object sender, EventArgs e)
55+
{
56+
var items = (IEnumerable)this.tdg.ItemsSource;
57+
this.expandedItems = new List<Data>();
58+
AddExpandedItems(items, expandedItems);
59+
}
60+
61+
private void AddExpandedItems(IEnumerable items, List<Data> expandedItems)
62+
{
63+
foreach (Data item in items)
64+
{
65+
if (this.tdg.IsExpanded(item))
66+
{
67+
expandedItems.Add(item);
68+
}
69+
70+
IEnumerable childrenOfTheItem = item.Children;
71+
AddExpandedItems(childrenOfTheItem, expandedItems);
72+
}
73+
}
74+
75+
private void Button2_Clicked(object sender, EventArgs e)
76+
{
77+
foreach (Data item in this.expandedItems)
78+
{
79+
this.tdg.Expand(item);
80+
}
81+
}
82+
}
83+
```
84+
85+
### Using `IsExpandableBinding`
86+
87+
As an alternative, use the [`IsExpandableBinding`](https://docs.telerik.com/devtools/maui/controls/treedatagrid/descriptor) property at the descriptor level:
88+
89+
1. Add a `bool` property in the data model to indicate whether an item is expandable.
90+
2. Bind this property to the `IsExpandableBinding` property of the TreeDataGrid descriptor.
91+
3. Implement the logic in button clicks to get and restore expanded items.
92+
93+
This approach provides direct control over the expandability of rows via data binding.
94+
95+
## See Also
96+
97+
- [TreeDataGrid Overview](https://docs.telerik.com/devtools/maui/controls/treedatagrid/overview)
98+
- [Expand and Collapse Methods in TreeDataGrid](https://docs.telerik.com/devtools/maui/controls/treedatagrid/methods)

0 commit comments

Comments
 (0)