Columns in ASP.NET MVC Tree Grid Component

21 Dec 202314 minutes to read

The column definitions are used as the DataSource schema in the TreeGrid. This plays a vital role in rendering column values in the required format.
The treegrid operations such as sorting, filtering and searching etc. are performed based on column definitions. The Field property of the Columns
is necessary to map the data source values in TreeGrid Columns.

NOTE

  1. If the column Field is not specified in the dataSource, the column values will be empty.

    2. If the Field name contains “dot” operator, it is considered as complex binding.

TreeColumnIndex property denotes the column that is used to expand and collapse child rows.

Format

To format cell values based on specific culture, use the Format property of Column. The TreeGrid uses Internalization library to format number and date
values.

@using Syncfusion.EJ2.Grids

@(Html.EJS().TreeGrid("Formatting").AllowPaging()
      .DataSource((IEnumerable<object>)ViewBag.datasource)
      .Columns(col =>
       {
        col.Field("ID").HeaderText("Order ID").Width(90).TextAlign(TextAlign.Right).Add();
        col.Field("Name").HeaderText("Order Name").Width(180).Add();
        col.Field("Price").HeaderText("Price").Format("yMd").TextAlign(TextAlign.Right).Width(90).Format("c2").Add();

       }).Height(315).ChildMapping("Children").TreeColumnIndex(1).Render()
)
public ActionResult Formatting()
{
    var treeData = ShipmentData.GetShipmentData();
    ViewBag.datasource = treeData;
    return View();    
}

NOTE

By default, the number and date values are formatted in en-US locale.

Number formatting

The number or integer values can be formatted using the below format strings.

Format Description Remarks
N Denotes numeric type. The numeric format is followed by integer value as N2, N3. etc which denotes the number of precision to be allowed.
C Denotes currency type. The currency format is followed by integer value as C2, C3. etc which denotes the number of precision to be allowed.
P Denotes percentage type The percentage format expects the input value to be in the range of 0 to 1. For example the cell value 0.2 is formatted as 20%. The percentage format is followed by integer value as P2, P3. etc which denotes the number of precision to be allowed.

Refer to the link to know more about Number formatting.

Date formatting

You can format date values either using built-in date format string or custom format string.

For built-in date format you can specify Format property as string (Example: yMd). Refer to the link to know more about Date formatting.

You can also use custom format string to format the date values. Some of the custom formats and the formatted date values are given in the below table.

Format Formatted value
{ type:’date’, format:’dd/MM/yyyy’ } 04/07/1996
{ type:’date’, format:’dd.MM.yyyy’ } 04.07.1996
{ type:’date’, skeleton:’short’ } 7/4/96
{ type: ‘dateTime’, format: ‘dd/MM/yyyy hh:mm a’ } 04/07/1996 12:00 AM
{ type: ‘dateTime’, format: ‘MM/dd/yyyy hh:mm:ss a’ } 07/04/1996 12:00:00 AM
@using Syncfusion.EJ2.Grids

@(Html.EJS().TreeGrid("DateFormatting")
      .DataSource((IEnumerable<object>)ViewBag.datasource)
      .Columns(col =>
       {
         col.Field("ID").HeaderText("Order ID").Width(90).TextAlign(TextAlign.Right).Add();
         col.Field("Name").HeaderText("Order Name").Width(180).Add();
         col.Field("OrderDate").HeaderText("Order Date").Format("yMd").TextAlign(TextAlign.Right).Width(90).Add();
         col.Field("Price").HeaderText("Price").TextAlign(TextAlign.Right).Width(80).Format("C2").Add();

       }).Height(315).ChildMapping("Children").TreeColumnIndex(1).Render()
)
public ActionResult Dateformatting()
{
    var treeData = ShipmentData.GetShipmentData();
    ViewBag.datasource = treeData;
    return View();    
}

Lock columns

You can lock columns by using LockColumn property of Column. The locked columns will be moved to the first position. Also you can’t reorder its position.

In the below example, Duration column is locked and its reordering functionality is disabled.

@using Syncfusion.EJ2.Grids

@(Html.EJS().TreeGrid("Lock")
      .DataSource((IEnumerable<object>)ViewBag.datasource)
      .Columns(col =>
       {
         col.Field("TaskId").HeaderText("Task ID").Width(90).TextAlign(TextAlign.Right).Add();
         col.Field("TaskName").HeaderText("Task Name").Width(180).Add();
         col.Field("StartDate").HeaderText("Start Date").Format("yMd").TextAlign(TextAlign.Right).Width(90).Add();
         col.Field("Duration").HeaderText("Duration").Width(80).TextAlign(TextAlign.Right).LockColumn(true).Add();

       }).Height(315).ChildMapping("Children").AllowReordering().TreeColumnIndex(2).Render()
)
public ActionResult Lock()
{
    var treeData = TreeGridItems.GetTreeData();
    ViewBag.datasource = treeData;
    return View();    
}

Column type

Column type can be specified using the Type property in Column. It specifies the type of data the column binds.

If the Format is defined for a column, the column uses Type to select the appropriate format option (number
or date).

TreeGrid column supports the following types:

  • string
  • number
  • boolean
  • date
  • datetime

NOTE

If the Type is not defined, it will be determined from the first record of the DataSource.

Checkbox column

To render checkboxes in existing column, you need to set ShowCheckbox property as true.

It is also possible to select the rows hierarchically using checkboxes in TreeGrid by enabling AutoCheckHierarchy property. When we check on any parent record checkbox then the child record checkboxes will get checked.

@using Syncfusion.EJ2.Grids

@(Html.EJS().TreeGrid("CheckboxSelection")
            .Height(400)
            .DataSource((IEnumerable<object>)ViewBag.datasource)
            .AutoCheckHierarchy(true)
            .Columns(col =>
            {
                col.Field("TaskId").HeaderText("Task ID").Width(110).TextAlign(TextAlign.Right).Add();
                col.Field("TaskName").HeaderText("Task Name").ShowCheckbox(true).Width(210).Add();
                col.Field("StartDate").HeaderText("Start Date").TextAlign(TextAlign.Right).Format("yMd").Width(210).Add();
                col.Field("Duration").HeaderText("Duration").TextAlign(TextAlign.Right).Width(190).Add();
            })
            .ChildMapping("Children").TreeColumnIndex(1).Render()
)
public IActionResult Index()
{
    var tree = TreeData.GetDefaultData();
    ViewBag.datasource = tree;
    return View();
}

Controlling Tree Grid actions

You can enable or disable treegrid action for a particular column by setting the AllowFiltering, and AllowSorting properties in Column.

@using Syncfusion.EJ2.Grids

@(Html.EJS().TreeGrid("GridAction").AllowFiltering().AllowSorting()
      .DataSource((IEnumerable<object>)ViewBag.datasource)
      .Columns(col =>
       {
         col.Field("TaskId").HeaderText("Task ID").Width(90).AllowSorting(false).TextAlign(TextAlign.Right).Add();
         col.Field("TaskName").HeaderText("Task Name").Width(180).Add();
         col.Field("StartDate").HeaderText("Start Date").Format("yMd").AllowFiltering(false).TextAlign(TextAlign.Right).Width(90).Add();
         col.Field("Duration").HeaderText("Duration").Width(80).Add();

       }).Height(270).ChildMapping("Children").TreeColumnIndex(1).Render()
)
public ActionResult GridAction()
{
    var treeData = TreeGridItems.GetTreeData();
    ViewBag.datasource = treeData;
    return View();    
}

Show/hide columns by external button

You can show or hide treegrid columns dynamically using external buttons by invoking the showColumns or hideColumns method.

@using Syncfusion.EJ2.Grids

@Html.EJS().Button("button1").Content("SHOW").Render()

@Html.EJS().Button("button2").Content("HIDE").Render()

@(Html.EJS().TreeGrid("ShowHide").DataSource((IEnumerable<object>)ViewBag.datasource)
            .Columns(col =>
             {
               col.Field("TaskId").HeaderText("Task ID").Width(90).TextAlign(TextAlign.Right).Add();
               col.Field("TaskName").HeaderText("Task Name").Width(180).Add();
               col.Field("StartDate").HeaderText("Start Date").Format("yMd").TextAlign(TextAlign.Right).Width(120).Add();
               col.Field("Duration").HeaderText("Duration").Width(80).TextAlign(TextAlign.Right).Add();

             }).Height(270).ChildMapping("Children").TreeColumnIndex(1).Render()
)

<script>
    document.getElementById("button1").addEventListener('click', () => {
        var treegrid = document.getElementById("ShowHide").ej2_instances[0];
        treegrid.showColumns(['Task ID', 'Duration']); //show by HeaderText
    });
    document.getElementById("button2").addEventListener('click', () => {
        var treegrid = document.getElementById("ShowHide").ej2_instances[0];
        treegrid.hideColumns(['Task ID', 'Duration']); //hide by HeaderText
    });
</script>
public ActionResult ShowHide()
{
    var treeData = TreeGridItems.GetTreeData();
    ViewBag.datasource = treeData;
    return View();    
}

ValueAccessor

The ValueAccessor is used to access/manipulate the value of display data. You can achieve custom value formatting by using the ValueAccessor.

@using Syncfusion.EJ2.Grids

@(Html.EJS().TreeGrid("Value").DataSource((IEnumerable<object>)ViewBag.datasource)
      .Columns(col =>
      {
          col.Field("orderID").HeaderText("Order ID").Width(90).TextAlign(TextAlign.Right).Add();
          col.Field("orderName").HeaderText("Order Name").Width(180).ValueAccessor("orderFormatter").Add();
          col.Field("orderDate").HeaderText("Order Date").Format("yMd").TextAlign(TextAlign.Right).Width(90).Add();
          col.Field("price").HeaderText("Price").ValueAccessor("currencyFormatter").TextAlign(TextAlign.Right).Width(80).Add();

      }).Height(315).ChildMapping("subTasks").TreeColumnIndex(1).Render()
)

<script>
    function currencyFormatter(field, data, column) {
        return '€' + data['price'];
    }

    function orderFormatter(field, data, column) {
        return data[field] + '-' + data['category'];
    }
</script>
public ActionResult Value()
{
    var treeData = TreeGridItems.GetTreeData();
    ViewBag.datasource = treeData;
    return View();    
}

Display array type columns

You can bind an array of objects in a column by using the ValueAccessor property.
In this example, the name field has an array of two objects, FirstName and LastName. These two objects are joined and bound to a column using the
ValueAccessor.

@using Syncfusion.EJ2.Grids

@(Html.EJS().TreeGrid("ValueArray").DataSource((IEnumerable<object>)ViewBag.datasource)
      .Columns(col =>
       {
         col.Field("TaskId").HeaderText("Task ID").Width(90).TextAlign(TextAlign.Right).Add();
         col.Field("TaskName").HeaderText("Task Name").Width(180).Add();
         col.Field("name").HeaderText("Assignee").Width(90).ValueAccessor("orderFormatter").TextAlign(TextAlign.Right).Add();
         col.Field("Duration").HeaderText("Duration").Width(80).TextAlign(TextAlign.Right).Add();

       }).Height(315).ChildMapping("Children").TreeColumnIndex(1).Render()
)

<script>
    function orderFormatter(field, data, column) {
        return data[field].map(function (s) {
            return s.lastName || s.firstName
        }).join(' ');
    }
</script>
public ActionResult ValueArray()
{
    var treeData = TreeGridItems.GetTreeData();
    ViewBag.datasource = treeData;
    return View();    
}

Expression column

You can achieve the expression column by using the ValueAccessor property.

@using Syncfusion.EJ2.Grids

@(Html.EJS().TreeGrid("Expression").DataSource((IEnumerable<object>)ViewBag.datasource)
      .Columns(col =>
       {
         col.Field("orderID").HeaderText("Order ID").Width(90).TextAlign(TextAlign.Right).Add();
         col.Field("orderName").HeaderText("Order Name").Width(180).Add();
         col.Field("units").HeaderText("Units").TextAlign(TextAlign.Right).Width(90).Add();
         col.Field("unitPrice").HeaderText("Unit Price").TextAlign(TextAlign.Right).Width(80).Format("c2").Add();
         col.Field("price").HeaderText("Price").TextAlign(TextAlign.Right).ValueAccessor("totalPrice").Width(80).Format("c2").Add();

       }).Height(315).ChildMapping("subTasks").TreeColumnIndex(1).Render()
)

<script>
    function totalPrice(field, data, column) {
        return data.units * data.unitPrice;
    };
</script>
public ActionResult Expression()
{
    var treeData = TreeGridItems.GetTreeData();
    ViewBag.datasource = treeData;
    return View();
}

How to render boolean values as checkbox

To render boolean values as checkbox in columns, you need to set DisplayAsCheckBox property as true.

@using Syncfusion.EJ2.Grids

@(Html.EJS().TreeGrid("DisplayAsCheckbox").DataSource((IEnumerable<object>)ViewBag.datasource)
      .Columns(col =>
       {
         col.Field("TaskId").HeaderText("Task ID").Width(90).TextAlign(TextAlign.Right).Add();
         col.Field("TaskName").HeaderText("Task Name").Width(180).Add();
         col.Field("Approved").HeaderText("Approved").DisplayAsCheckBox(true).Width("90").Add();
         col.Field("Duration").HeaderText("Duration").Width(80).Add();

       }).Height(315).ChildMapping("Children").TreeColumnIndex(1).Render()
)
public ActionResult DisplayAsCheckbox()
{
    var treeData = TreeGridItems.GetTreeData();
    ViewBag.datasource = treeData;
    return View();    
}

NOTE

You can refer to our ASP.NET MVC Tree Grid feature tour page for its groundbreaking feature representations. You can also explore our ASP.NET MVC Tree Grid example to knows how to present and manipulate data.