Context menu

25 Feb 20227 minutes to read

The TreeGrid has options to show the context menu when right clicked on it. To enable this feature, you need to define either default or custom item in the ContextMenuItems.

The default items are in the following table.

Items Description
AutoFit Auto fit the current column.
AutoFitAll Auto fit all columns.
Edit Edit the current record.
Delete Delete the current record.
Save Save the edited record.
Cancel Cancel the edited state.
PdfExport Export the treegrid data as Pdf document.
ExcelExport Export the treegrid data as Excel document.
CsvExport Export the treegrid data as CSV document.
SortAscending Sort the current column in ascending order.
SortDescending Sort the current column in descending order.
FirstPage Go to the first page.
PrevPage Go to the previous page.
LastPage Go to the last page.
NextPage Go to the next page.
AddRow Add new row to the treegrid.
      .EditSettings(edit => edit.AllowAdding(true).AllowDeleting(true).AllowEditing(true).Mode(Syncfusion.EJ2.TreeGrid.EditMode.Row))
      .PageSettings(page => page.PageSize(7))
          new List<object>() { "SortAscending",
              "SortDescending", "Edit", "Delete",
              "Save", "Cancel", "PdfExport", "ExcelExport", "CsvExport",
              "FirstPage", "PrevPage", "LastPage", "NextPage"
      .Columns(col =>
         col.Field("TaskId").HeaderText("Task ID").Width(90).IsPrimaryKey(true).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").EditType("datepickeredit")

public IActionResult Index()
    var tree = TreeData.GetDefaultData();
    ViewBag.datasource = tree;
    return View();

Custom context menu items

The custom context menu items can be added by defining the ContextMenuItems as a collection of ContextMenuItemModel.
Actions for this customized items can be defined in the ContextMenuClick event.

In the below sample, we have shown context menu item for parent rows to expand or collapse child rows.

    List<object> ContextMenuitems = new List<object>();
    ContextMenuitems.Add(new { text = "Collapse the Row", target = ".e-content", id = "collapserow" });
    ContextMenuitems.Add(new { text = "Expand the Row", target = ".e-content", id = "expandrow" });

      .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(90).Add();


    function ContextMenuOpen(args) {
        var treegrid = document.getElementById("TreeGrid").ej2_instances[0];
        var elem =;
        var uid = elem.closest('.e-row').getAttribute('data-uid');
        if (ej.base.isNullOrUndefined(ej.base.getValue('hasChildRecords', treegrid.grid.getRowObjectFromUID(uid).data))) {
            args.cancel = true;
        } else {
            var flag = ej.base.getValue('expanded', treegrid.grid.getRowObjectFromUID(uid).data);
            var val = flag ? 'none' : 'block';
            document.querySelectorAll('li#expandrow')[0].setAttribute('style', 'display: ' + val + ';');
            val = !flag ? 'none' : 'block';
            document.querySelectorAll('li#collapserow')[0].setAttribute('style', 'display: ' + val + ';');
    function ContextMenuClick(args) {
        var treegrid = document.getElementById("TreeGrid").ej2_instances[0];
        if ( === 'collapserow') {
            treegrid.collapseRow((treegrid.getSelectedRows()[0]), treegrid.getSelectedRecords()[0]);
        } else {
            treegrid.expandRow((treegrid.getSelectedRows()[0]), treegrid.getSelectedRecords()[0]);
public IActionResult Index()
    var tree = TreeData.GetDefaultData();
    ViewBag.datasource = tree;
    return View();

You can hide or show an item in context menu for specific area inside of treegrid by defining the Target property.
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.