AutoFit Specific Columns in ASP.NET MVC Tree Grid Component

21 Dec 20221 minute to read

The autoFitColumns method resizes the column to fit the widest cell’s content without wrapping. You can autofit a specific column at initial rendering by invoking the autoFitColumns method in DataBound event.

@using Syncfusion.EJ2.Grids

@(Html.EJS().TreeGrid("AutoFit").AllowResizing()
      .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(60).Add();
         col.Field("StartDate").HeaderText("Start Date").Format("yMd").TextAlign(TextAlign.Right).Width(120).Add();
         col.Field("Duration").HeaderText("Duration").Width(120).TextAlign(TextAlign.Right).Add();
         col.Field("Progress").HeaderText("Progress").Width(120).TextAlign(TextAlign.Right).Add();

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

<script>
    function dataBound(args) {
        var treegrid = document.getElementById("AutoFit").ej2_instances[0];
        treegrid.autoFitColumns(['TaskName']);
    }
</script>
public ActionResult AutoFit()
{
    var treeData = TreeGridItems.GetTreeData();
    ViewBag.datasource = treeData;
    return View();    
}

NOTE

You can autofit all the columns by invoking the autoFitColumns method without column names.

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.