# Getting Started with the .NET MAUI TreeDataGrid

This guide provides the information you need to start using the [Telerik UI for .NET MAUI TreeDataGrid](https://www.telerik.com/maui-ui/documentation/controls/treedatagrid/overview) by adding the control to your project.

At the end, you will achieve the following result.

![.NET MAUI TreeDataGrid Getting Started](https://www.telerik.com/maui-ui/documentation/assets/e8babd6eac985d8138f120f2b12604db/treedatagrid-getting-started.png)

## Prerequisites

Before adding the TreeDataGrid, you need to:

1. [Set up your .NET MAUI application](https://www.telerik.com/maui-ui/documentation/get-started/quickstart#prerequisites).

2. [Set Up Telerik Development Environment](https://www.telerik.com/maui-ui/documentation/get-started/quickstart#set-up-telerik-development-environment)

## Define the Control

The TreeDataGrid uses UI virtualization and requires its parent container to provide a defined vertical or horizontal space. Virtualization will not work if the TreeDataGrid is measured with infinite width or height.

Avoid placing the TreeDataGrid in the following layouts, as this will prevent virtualization from functioning:

* Do not place the TreeDataGrid inside a `StackLayout` that is wrapped in a `ScrollView`.
* Do not place the TreeDataGrid directly inside a `ScrollView`.

Place the TreeDataGrid control inside a `Grid` layout with a row height set to `*` (star) or a specific value.

By default, the TreeDataGrid auto-generates rows depending on the number of objects in the collection set as its `ItemsSource`.

**1.** When your .NET MAUI application is set up, you are ready to add a TreeDataGrid control to your page.

```XAML
<telerik:RadTreeDataGrid x:Name="treeDataGrid"
						 ItemsSource="{Binding Items}"
						 AutoGenerateColumns="False">
	<telerik:RadTreeDataGrid.ItemDescriptor>
		<telerik:TreeDataGridItemDescriptor ItemsSourceBinding="{Binding Children}" />
	</telerik:RadTreeDataGrid.ItemDescriptor>
	<telerik:RadTreeDataGrid.BindingContext>
		<local:ViewModel />
	</telerik:RadTreeDataGrid.BindingContext>
	<telerik:RadTreeDataGrid.Columns>
		<telerik:DataGridTextColumn PropertyName="Name" />
		<telerik:DataGridNumericalColumn PropertyName="Size" />
		<telerik:DataGridTextColumn PropertyName="Type" />
	</telerik:RadTreeDataGrid.Columns>
</telerik:RadTreeDataGrid>
```

**2.** Add the `telerik` namespace:

```XAML
xmlns:telerik="http://schemas.telerik.com/2022/xaml/maui"
```

**3.** Create a sample `Data` class:

```C#
public class Data
{
	public Data(string name, int size, string type)
	{
		this.Name = name;
		this.Size = size;
		this.Type = type;
		this.Children = new ObservableCollection<Data>();
	}

	public string Name { get; set; }
	public int Size { get; set; }
	public string Type { get; set; }
	public ObservableCollection<Data> Children { get; set; }
}
```

**4.** Add the `ViewModel` class:

```C#
public class ViewModel
{
	public ViewModel()
	{
		Items = new ObservableCollection<Data>();
		Items.Add(new Data("My Projects", 234 ,"Folder")
		{
			Children = new ObservableCollection<Data>()
				{
					new Data("Using latest Telerik .NET MAUI controls", 234 ,"Folder")
					{
						Children = new ObservableCollection<Data>()
						{
							new Data("TreeDataGrid", 6, "File"),
							new Data("CollectionView", 6, "File"),
							new Data("DataGrid", 54, "File"),
							new Data("Scheduler", 12, "File"),
							new Data("TreeView", 2, "File"),
							new Data("Calendar", 23, "File"),
							new Data("RichTextEditor", 0, "File"),
							new Data("PdfViewer", 55, "File"),
							new Data("ToggleButton", 21, "File"),
							new Data("TemplatedButton", 88, "File"),
						}
					},
					new Data("Blank project", 0, "Folder")
				}
		});
		Items.Add(new Data("Shared Documents", 643, "Folder")
		{
			Children = new ObservableCollection<Data>()
				{
					new Data("Reports", 643, "Folder")
					{
						Children = new ObservableCollection<Data>()
						{
							new Data("October", 234, "File"),
							new Data("November", 0, "File"),
							new Data("December", 409, "File")
						}
					}
				}
		});
		Items.Add(new Data("Pictures", 643, "Folder")
		{
			Children = new ObservableCollection<Data>()
				{
					new Data("Camera Roll", 231, "Folder")
					{
						Children = new ObservableCollection<Data>()
						{
							new Data("hello.png", 107, "File"),
							new Data("happy_summer.png", 0, "File"),
							new Data("avatar.png", 124, "File")
						}
					},
					new Data("Saved Pictures", 453, "Folder")
					{
						Children = new ObservableCollection<Data>()
						{
							new Data("vacation.png", 234, "File"),
							new Data("november.png", 0, "File"),
							new Data("mountains.png", 227, "File")
						}
					}
				}
		});
		Items.Add(new Data("Documents", 876, "Folder")
		{
			Children = new ObservableCollection<Data>()
				{
					new Data("Internal Usage Only", 643, "Folder")
					{
						Children = new ObservableCollection<Data>()
						{
							new Data("reports.docx", 234, "File"),
							new Data("confidential.xlsx", 0, "File"),
							new Data("internal_usage.pdf", 409, "File")
						}
					}
				}
		});
	}
	public ObservableCollection<Data> Items { get; set; }
}
```

**5.** Register the Telerik controls through the `Telerik.Maui.Controls.Compatibility.UseTelerik` extension method called inside the `CreateMauiApp` method of the `MauiProgram.cs` file of your project:

```C#
using Telerik.Maui.Controls.Compatibility;

public static class MauiProgram
{
	public static MauiApp CreateMauiApp()
	{
		var builder = MauiApp.CreateBuilder();
		builder
			.UseTelerik()
			.UseMauiApp<App>()
			.ConfigureFonts(fonts =>
			{
				fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
			});

		return builder.Build();
	}
}           
```

> For a runnable example with the TreeDataGrid Getting Started scenario, see the [SDKBrowser Demo Application](https://www.telerik.com/maui-ui/documentation/demos-and-sample-apps/sdkbrowser-app) and go to **TreeDataGrid > Getting Started** category.

## Additional Resources

* [Setting the .NET MAUI TreeDataGrid Columns](https://www.telerik.com/maui-ui/documentation/controls/treedatagrid/columns/overview)
* [Sorting .NET MAUI TreeDataGrid Records](https://www.telerik.com/maui-ui/documentation/controls/treedatagrid/sorting)
* [Filtering .NET MAUI TreeDataGrid Records](https://www.telerik.com/maui-ui/documentation/controls/treedatagrid/filtering/overview)

## See Also

* [.NET MAUI TreeDataGrid Product Page](https://www.telerik.com/maui-ui/treedatagrid)
* [.NET MAUI TreeDataGrid Forum Page](https://www.telerik.com/forums/maui?tagId=1801)
* [Telerik .NET MAUI Blogs](https://www.telerik.com/blogs/mobile-net-maui)
* [Telerik .NET MAUI Roadmap](https://www.telerik.com/support/whats-new/maui-ui/roadmap)
