Search results

Cell

Displaying the HTML content

The HTML tags can be displayed in the TreeGrid header and content by enabling the DisableHtmlEncode property.

razor
htmlEncode.cs
@Html.EJS().TreeGrid("TreeGrid").Height(300).DataSource((IEnumerable<object>)ViewBag.datasource).Columns(col =>
{
   col.Field("TaskId").HeaderText("<span> Task ID </span>").Width(110).DisableHtmlEncode(true).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
   col.Field("TaskName").HeaderText("<span> Task Name </span>").DisableHtmlEncode(true).Width(210).Add();
   col.Field("StartDate").HeaderText("Start Date").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Format("yMd").Width(210).Add();
   col.Field("Duration").HeaderText("Duration").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Width(110).Add();

}).ChildMapping("Children").AllowPaging(true).TreeColumnIndex(1).Render()
public IActionResult Index()
{
    var tree = TreeData.GetDefaultData();
    ViewBag.datasource = tree;
    return View();
}

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.

razor
queryCell.cs
@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();
}