Syncfusion AI Assistant

How can I help you?

Getting Started with ASP.NET Core TreeGrid Control

26 May 202611 minutes to read

This section explains how to add the ASP.NET Core TreeGrid control to an ASP.NET Core application using Visual Studio.

Prerequisites

System requirements for ASP.NET Core controls

Create ASP.NET Core web application with Razor pages

Create an ASP.NET Core Razor Pages project using Visual Studio:

  1. Start Visual Studio and select Create a new project.

  2. In the Create a new project window, choose ASP.NET Core Web App (Razor Pages)Next.

  3. In the Configure your new project dialog, specify the project name as RazorPagesTreeGrid (and optionally change location/folder).

  4. Click Next.

  5. In the Additional information dialog:
    • Select .NET 10.0.
    • Verify: Do not use top-level statements is unchecked.
  6. Click Create.

For alternative approaches to create the project, see Create a new project in Visual Studio.

Install ASP.NET Core package in the application

To add ASP.NET Core controls in the application, open the NuGet package manager in Visual Studio (Tools → NuGet Package Manager → Manage NuGet Packages for Solution), search for Syncfusion.EJ2.AspNet.Core and then install it. Alternatively, you can utilize the following package manager command to achieve the same.

Install-Package Syncfusion.EJ2.AspNet.Core -Version 33.2.3

Create an ASP.NET Core Razor Pages project using Visual Studio Code:

  • Install the latest .NET SDK that supports .NET 10.0 or later.
  • Open Visual Studio Code.
  • Press Ctrl + ` to open the integrated terminal.
  • Run the following commands:
dotnet new webapp -o RazorPagesTreeGrid

code -r RazorPagesTreeGrid

Install ASP.NET Core package in the application

To integrate the Syncfusion® ASP.NET Core DataGrid component, install the required Syncfusion.EJ2.AspNet.Core NuGet packages using the integrated terminal:

  1. Press Ctrl + ` to open the integrated terminal in Visual Studio Code.
  2. Navigate to the directory containing the .csproj file.
  3. Run the following commands to install the packages:
dotnet add package Syncfusion.EJ2.AspNet.Core --version 33.2.3

NOTE

Syncfusion® ASP.NET Core controls are available in nuget.org. Refer to NuGet packages topic to learn more about installing NuGet packages in various OS environments. The Syncfusion.EJ2.AspNet.Core NuGet package has dependencies, Newtonsoft.Json for JSON serialization and Syncfusion.Licensing for validating Syncfusion® license key.

Add Syncfusion® ASP.NET Core Tag Helper

Open ~/Pages/_ViewImports.cshtml file and import the Syncfusion.EJ2 TagHelper.

@addTagHelper *, Syncfusion.EJ2

Add stylesheet and script resources

Here, the theme and script is referred using CDN inside the <head> of ~/Pages/Shared/_Layout.cshtml file as follows,

<head>
    ...
    <!-- Syncfusion ASP.NET Core controls styles -->
    <link rel="stylesheet" href="https://cdn.syncfusion.com/ej2/33.2.3/fluent.css" />
    <!-- Syncfusion ASP.NET Core controls scripts -->
    <script src="https://cdn.syncfusion.com/ej2/33.2.3/dist/ej2.min.js"></script>
</head>

NOTE

Checkout the Themes topic to learn different ways (CDN, NPM package, and CRG) to refer styles in ASP.NET Core application, and to have the expected appearance for Syncfusion® ASP.NET Core controls.

NOTE

Checkout the Adding Script Reference topic to learn different approaches for adding script references in your ASP.NET Core application.

Register Syncfusion® Script Manager

Also, register the script manager <ejs-script> at the end of <body> of ~/Pages/Shared/_Layout.cshtml file as follows.

<body>
    ...
    <!-- Syncfusion ASP.NET Core Script Manager -->
    <ejs-scripts></ejs-scripts>
</body>

The <ejs-scripts> tag must be placed AFTER all page content to ensure all Syncfusion controls are fully rendered before the script manager initializes them.

Add ASP.NET Core TreeGrid control

Now, add the Syncfusion® ASP.NET Core TreeGrid tag helper in ~/Pages/Index.cshtml page.

@page
@model RazorPagesTreeGrid.Pages.IndexModel

<ejs-treegrid id="TreeGrid" dataSource="@Model.TreeData" childMapping="Children" treeColumnIndex="1">

    <e-treegrid-columns>
        <e-treegrid-column field="TaskId" headerText="Task ID" textAlign="Right" width="95"></e-treegrid-column>
        <e-treegrid-column field="TaskName" headerText="Task Name" width="220"></e-treegrid-column>
        <e-treegrid-column field="StartDate" headerText="Start Date" format="yMd" type="date" textAlign="Right" width="115"></e-treegrid-column>
        <e-treegrid-column field="EndDate" headerText="End Date" format="yMd" type="date" textAlign="Right" width="115"></e-treegrid-column>
        <e-treegrid-column field="Duration" headerText="Duration" textAlign="Right" width="100"></e-treegrid-column>
    </e-treegrid-columns>

</ejs-treegrid>
public class IndexModel : PageModel
{
    public List<TreeGridItems> TreeData { get; set; }

    public void OnGet()
    {
        TreeData = TreeGridItems.GetTreeData();
    }
}

public class TreeGridItems
{
    public int? TaskId { get; set; }
    public string? TaskName { get; set; }
    public DateTime? StartDate { get; set; }
    public DateTime? EndDate { get; set; }
    public int? Duration { get; set; }
    public List<TreeGridItems>? Children { get; set; }

    public TreeGridItems() { }

    public TreeGridItems(int taskId, string taskName, DateTime start, DateTime end, int duration)
    {
        TaskId = taskId;
        TaskName = taskName;
        StartDate = start;
        EndDate = end;
        Duration = duration;
    }

    public static List<TreeGridItems> GetTreeData()
    {
        List<TreeGridItems> data = new List<TreeGridItems>();

        var parent1 = new TreeGridItems
        {
            TaskId = 1,
            TaskName = "Planning",
            StartDate = new DateTime(2025, 2, 4),
            EndDate = new DateTime(2025, 2, 7),
            Duration = 4,
            Children = new List<TreeGridItems>()
        };

        parent1.Children.Add(new TreeGridItems(2, "Plan timeline", new DateTime(2025, 2, 4), new DateTime(2025, 2, 7), 4));
        parent1.Children.Add(new TreeGridItems(3, "Plan budget", new DateTime(2025, 2, 4), new DateTime(2025, 2, 7), 4));

        var parent2 = new TreeGridItems
        {
            TaskId = 4,
            TaskName = "Design",
            StartDate = new DateTime(2025, 2, 10),
            EndDate = new DateTime(2025, 2, 14),
            Duration = 5,
            Children = new List<TreeGridItems>()
        };

        parent2.Children.Add(new TreeGridItems(5, "Software Specification", new DateTime(2025, 2, 10), new DateTime(2025, 2, 12), 3));
        parent2.Children.Add(new TreeGridItems(6, "Design Documentation", new DateTime(2025, 2, 13), new DateTime(2025, 2, 14), 2));
        parent2.Children.Add(new TreeGridItems(7, "Design complete", new DateTime(2025, 2, 14), new DateTime(2025, 2, 14), 1));

        data.Add(parent1);
        data.Add(parent2);

        return data;
    }
}

Press Ctrl+F5 (Windows) or +F5 (macOS) to run the app. Then, the Syncfusion® ASP.NET Core TreeGrid control will be rendered in the default web browser.

ASP.NET Core TreeGrid Columns

See also