Cell in ASP.NET MVC Tree Grid Component

21 Dec 20227 minutes to read

Customize cell styles

The appearance of cells can be customized by using the QueryCellInfo event.
The QueryCellInfo event triggers for every cell. In that event handler, you can get QueryCellInfoEventArgs that contains the details of the cell.

@Html.EJS().TreeGrid("TreeGrid").Height(300).DataSource((IEnumerable<object>)ViewBag.datasource).QueryCellInfo("customizeCell").Columns(col =>
{
   col.Field("TaskId").HeaderText("Task ID").Width(110).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
   col.Field("TaskName").HeaderText("Task Name").Width(210).Add();
   col.Field("Duration").HeaderText("Duration").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Width(110).Add();
   col.Field("Progress").HeaderText("Progress").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Width(90).Add();

}).ChildMapping("Children").TreeColumnIndex(1).AllowPaging().Render()

<script>
    function customizeCell(QueryCellInfoEventArgs) {
        var args = QueryCellInfoEventArgs;
        if (args.column.field === 'Progress' && +args.cell.innerHTML > 90 && +args.cell.innerHTML <= 100) {
            args.cell.setAttribute('style', 'background-color:#336c12;color:white;');
        } else if (+args.cell.innerHTML > 20 && args.column.field === 'Progress') {
            args.cell.setAttribute('style', 'background-color:#7b2b1d;color:white;');
        }
    }
</script>
public IActionResult Index()
{
    var tree = TreeData.GetDefaultData();
    ViewBag.datasource = tree;
    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.