Search results

Scrolling in ASP.NET MVC Tree Grid control

The scrollbar will be displayed in the treegrid when content exceeds the element Width or Height. The vertical and horizontal scrollbars will be displayed based on the following criteria:

The vertical scrollbar appears when the total height of rows present in the treegrid exceeds its element height. The horizontal scrollbar appears when the sum of columns width exceeds the treegrid element width. The Height and Width are used to set the treegrid height and width, respectively.

The default value for Height and Width is auto.

Set width and height

To specify the Width and Height of the scroller in the pixel, set the pixel value to a number.

razor
width-height.cs
@(Html.EJS().TreeGrid("TreeGrid")
      .DataSource((IEnumerable<object>)ViewBag.datasource)
      .Columns(col =>
       {
        col.Field("TaskId").HeaderText("Task ID").Width(90).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
        col.Field("TaskName").HeaderText("Task Name").Width(180).Add();
        col.Field("StartDate").HeaderText("Start Date").Format("yMd").Type("date").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Width(120).Add();
        col.Field("Duration").HeaderText("Duration").Width(110).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();

       }).Height(315).Width(400).ChildMapping("Children").TreeColumnIndex(1).Render())
public IActionResult Index()
{
    var tree = TreeGridItems.GetTreeData();
    ViewBag.datasource = tree;
    return View();
}

Responsive with parent container

Specify the Width and Height as 100% to make the treegrid element fill its parent container. Setting the Height to 100% requires the treegrid parent element to have explicit height.

razor
responsive.cs
@(Html.EJS().TreeGrid("TreeGrid")
      .DataSource((IEnumerable<object>)ViewBag.datasource)
      .Columns(col =>
       {
         col.Field("TaskId").HeaderText("Task ID").Width(90).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
         col.Field("TaskName").HeaderText("Task Name").Width(180).Add();
         col.Field("StartDate").HeaderText("Start Date").Format("yMd").Type("date").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Width(120).Add();
         col.Field("Duration").HeaderText("Duration").Width(110).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();

       }).Height("100%").Width("100%").ChildMapping("Children").TreeColumnIndex(1).Render()
)
public IActionResult Index()
{
    var tree = TreeGridItems.GetTreeData();
    ViewBag.datasource = tree;
    return View();
}

Scroll to selected row

You can scroll the treegrid content to the selected row position by using the RowSelected event.

razor
selected-row.cs
@Html.EJS().NumericTextBox("numeric").Format("##").Min(0).Width(200).ShowSpinButton(false).Placeholder("Enter index to select a row").Change("onChange").Render()

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

       }).Height(270).Width("100%").ChildMapping("Children").TreeColumnIndex(1).RowSelected("rowSelected").Render()
)

<script>
    function onChange(args) {
        var treegrid = document.getElementById("TreeGrid").ej2_instances[0];
        treegrid.selectRow(parseInt(this.getText(), 10));
    }
    function rowSelected(args) {
        var rowHeight = this.getRows()[treegrid.getSelectedRowIndexes()[0]].scrollHeight;
        this.getContent().children[0].scrollTop = rowHeight * this.getSelectedRowIndexes()[0];
    }
</script>
public IActionResult Index()
{
    var tree = TreeGridItems.GetTreeData();
    ViewBag.datasource = tree;
    return View();
}

Frozen rows and columns

Frozen rows and columns provides an option to make rows and columns always visible in the top and left side of the treegrid while scrolling.

In this demo, the FrozenColumns is set as 2 and the FrozenRows is set as 3. Hence, the left two columns and top three rows are frozen.

razor
freezecolumn.cs
@Html.EJS().TreeGrid("DefaultFunctionalities").DataSource((IEnumerable<object>)ViewBag.datasource).AllowSelection(false).Columns(col =>
    {
        col.Field("TaskId").HeaderText("Task ID").Width(100).TextAlign(TextAlign.Right).Add();
        col.Field("TaskName").HeaderText("Task Name").Width(230).Add();
        col.Field("StartDate").HeaderText("Start Date").Width(150).TextAlign(TextAlign.Right).Format("yMd").Add();
        col.Field("EndDate").HeaderText("End Date").Width(150).TextAlign(TextAlign.Right).Format("yMd").Add();
        col.Field("Duration").HeaderText("Duration").Width(120).TextAlign(TextAlign.Right).Add();
        col.Field("Progress").HeaderText("Progress").Width(120).TextAlign(TextAlign.Right).Add();
        col.Field("Priority").HeaderText("Priority").Width(120).Add();
        col.Field("Approved").HeaderText("Approved").TextAlign(TextAlign.Left).Type("boolean").Width(110).Add();
    }).Height(410).ChildMapping("Children").TreeColumnIndex(1).FrozenColumns(2).FrozenRows(3).Render()
public IActionResult FrozenColumn()
        {
            ViewBag.datasource = TreeData.GetDefaultData();
            return View();
        }

Freeze particular columns

To freeze particular column in the treegrid, the IsFrozen property can be used.

In this demo, the columns with field name TaskName and StartDate is frozen using the IsFrozen property.

razor
isfreeze.cs
@Html.EJS().TreeGrid("DefaultFunctionalities").DataSource((IEnumerable<object>)ViewBag.datasource).AllowSelection(false).Columns(col =>
    {
        col.Field("TaskId").HeaderText("Task ID").Width(100).TextAlign(TextAlign.Right).Add();
        col.Field("TaskName").HeaderText("Task Name").Width(230).IsFrozen(true).Add();
        col.Field("StartDate").HeaderText("Start Date").Width(150).IsFrozen(true).TextAlign(TextAlign.Right).Format("yMd").Add();
        col.Field("EndDate").HeaderText("End Date").Width(150).TextAlign(TextAlign.Right).Format("yMd").Add();
        col.Field("Duration").HeaderText("Duration").Width(120).TextAlign(TextAlign.Right).Add();
        col.Field("Progress").HeaderText("Progress").Width(120).TextAlign(TextAlign.Right).Add();
        col.Field("Priority").HeaderText("Priority").Width(120).Add();
        col.Field("Approved").HeaderText("Approved").TextAlign(TextAlign.Left).Type("boolean").Width(110).Add();
    }).Height(410).ChildMapping("Children").Render()
public IActionResult FrozenColumn()
        {
            ViewBag.datasource = TreeData.GetDefaultData();
            return View();
        }

Limitations

The following features are not supported in frozen rows and columns:

  • Row Template
  • Detail Template