# Grid Hierarchy

The Grid component provides options for visualizing the relations between parent and child records by displaying data in a hierarchical manner through a detail template.

In this article:

* [Basics](https://www.telerik.com/blazor-ui/documentation/components/grid/hierarchy#basics)
* [Expand Rows From Code](https://www.telerik.com/blazor-ui/documentation/components/grid/hierarchy#expand-rows-from-code)
* [More Examples](https://www.telerik.com/blazor-ui/documentation/components/grid/hierarchy#more-examples)

## Basics

To implement hierarchy in the Grid, define a `DetailTemplate` under the main tag of the grid. In this template, you can access the model for the concrete row through the `context`, and use other components to show detailed data from it (for example, another grid, or any other set of components and HTML).

When a detail template is defined, an expand/collapse button is rendered at the beginning of the row that the user can click to show and hide the detailed data.

> caption Define a detail template to show hierarchical data from the model in a nested grid

<demo metaUrl="client/grid/hierarchy/" height="600"></demo>

> tip To have more levels, simply nest more grids and name the `context` variables. You can find an example in the [Multi-Level Hierarchy](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-three-level-hierarchy) KB article.

## Expand Rows From Code

You can choose which detail templates will be expanded from your code through the grid [state](https://www.telerik.com/blazor-ui/documentation/components/grid/state). Its `ExpandedItems` field contains a collection of the expanded Grid items (all detail templates are collapsed by default).

The `ExpandedItems` collection is compared against the Grid Data collection in order to determine which rows will be expanded. The default behavior of the framework is to compare objects by their reference.

When the `ExpandedItems` are obtained from a different data source to the Grid (e.g., from a separate service method and not from the view-model), the references may not match and so there will be no expanded items. In such cases, you have to override the [`Equals`](https://docs.microsoft.com/en-us/dotnet/api/system.object.equals) method of the underlying model class so that it matches them, for example, by a unique identifier rather than by reference so that two objects can be equal regardless of their origin, but according to their contents. When you are overriding the `Equals` method, it is also recommended to override the [`GetHashCode`](https://docs.microsoft.com/en-us/dotnet/api/system.object.gethashcode) method as well. A similar example is available at [Save and Load Grid State from Browser LocalStorage](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-save-load-state-localstorage).

> tip If you want to set an initial state to the Grid, use a similar snippet, but in the [`OnStateInit event`](https://www.telerik.com/blazor-ui/documentation/components/grid/state#onstateinit)

> caption Expand DetailTemplate hierarchy from code

```RAZOR
<TelerikGrid @ref="@GridRef"
             Data="@CategoryData"
             Pageable="true"
             PageSize="2">
    <GridToolBarTemplate>
        <TelerikDropDownList Data="@CategoryData"
                             @bind-Value="@DropDownListCategoryId"
                             TextField="@nameof(Category.Name)"
                             ValueField="@nameof(Category.Id)">
        </TelerikDropDownList>
        <TelerikButton ThemeColor="@ThemeConstants.Button.ThemeColor.Success"
                       OnClick="@ExpandCategory">Expand Category</TelerikButton>
        <span class="k-separator"></span>
        <TelerikButton ThemeColor="@ThemeConstants.Button.ThemeColor.Primary"
                       OnClick="@ExpandAll">Expand All Categories</TelerikButton>
        <span class="k-separator"></span>
        <TelerikButton OnClick="@CollapseAll">Collapse All Categories</TelerikButton>
    </GridToolBarTemplate>
    <GridColumns>
        <GridColumn Field="@(nameof(Category.Id))" Width="80px" />
        <GridColumn Field="@(nameof(Category.Name))" Title="Category Name" />
    </GridColumns>
    <DetailTemplate Context="category">
        <TelerikGrid Data="@ProductData.Where(x => x.CategoryId == category.Id)">
            <GridColumns>
                <GridColumn Field="@(nameof(Product.Name))" Title="Product Name" />
                <GridColumn Field="@(nameof(Product.Price))" DisplayFormat="{0:c2}" />
                <GridColumn Field="@(nameof(Product.Quantity))" />
            </GridColumns>
        </TelerikGrid>
    </DetailTemplate>
</TelerikGrid>

@code {
    private TelerikGrid<Category>? GridRef { get; set; }

    private List<Category> CategoryData { get; set; } = new();

    private List<Product> ProductData { get; set; } = new();

    private int DropDownListCategoryId { get; set; } = 1;

    private async Task ExpandCategory()
    {
        if (GridRef != null)
        {
            var gridState = GridRef.GetState();

            var categoryToExpand = CategoryData.First(x => x.Id == DropDownListCategoryId);

            gridState.ExpandedItems.Add(categoryToExpand);

            await GridRef.SetStateAsync(gridState);
        }
    }

    private async Task ExpandAll()
    {
        if (GridRef != null)
        {
            var gridState = GridRef.GetState();

            gridState.ExpandedItems = CategoryData;

            await GridRef.SetStateAsync(gridState);
        }
    }

    private async Task CollapseAll()
    {
        if (GridRef != null)
        {
            var gridState = GridRef.GetState();

            gridState.ExpandedItems.Clear();

            await GridRef.SetStateAsync(gridState);
        }
    }

    protected override void OnInitialized()
    {
        var categoryCount = 3;

        for (int i = 1; i <= categoryCount; i++)
        {
            CategoryData.Add(new Category()
            {
                Id = i,
                Name = $"Category {i}"
            });
        }

        for (int i = 1; i <= 12; i++)
        {
            ProductData.Add(new Product()
            {
                Id = i,
                CategoryId = i % categoryCount + 1,
                Name = $"Product {i}",
                Price = Random.Shared.Next(1, 100) * 1.23m,
                Quantity = Random.Shared.Next(0, 100)
            });
        }
    }

    public class Category
    {
        public int Id { get; set; }
        public string Name { get; set; } = string.Empty;
    }

    public class Product
    {
        public int Id { get; set; }
        public int CategoryId { get; set; }
        public string Name { get; set; } = string.Empty;
        public decimal Price { get; set; }
        public int Quantity { get; set; }
    }
}
```

## More Examples

The following articles and sample projects can be helpful when implementing hierarchy:

* [Editing in Hierarchy](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-edit-in-hierarchy)

* [Load Hierarchical Data On Demand](https://github.com/telerik/blazor-ui/tree/master/grid/load-on-demand-hierarchy)

* [Multi-Level Hierarchy](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-three-level-hierarchy)

* [Align Columns in Nested Grids](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-align-columns-hierarchy)

* [Custom Excel Export that Includes Hierarchy](https://github.com/telerik/blazor-ui/tree/master/grid/export-to-xlsx-hierarchy)

## Using Components in Grid Detail Templates

See specifics and example in this knowledge base article: [Using Components in Grid Templates](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-using-components-in-templates).

## See Also

* [Live Demo: Grid Hierarchy](https://demos.telerik.com/blazor-ui/grid/hierarchy)
* [Customize Hierarchy Expand Column in Blazor Grid](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-customize-hierarchy-expand-column-blazor-grid)
* [Drag and Drop Rows in Grid Hierarchy](https://www.telerik.com/blazor-ui/documentation/knowledge-base/grid-drag-drop-rows-hierarchy)
* [Blazor Grid](https://www.telerik.com/blazor-ui/documentation/components/grid/overview)
