Search results

Selection

Selection provides an option to highlight a row or a cell. It can be done through simple mouse down or arrow keys. To disable selection in the TreeGrid, set the AllowSelection to false.

The treegrid supports two types of selection that can be set by using the Type property in SelectionSettings. They are:

  • Single: The Single value is set by default, and it only allows selection of a single row or a cell.

  • Multiple: Allows you to select multiple rows or cells. To perform the multi-selection, press and hold CTRL key and click the desired rows or cells. To select range of rows or cells, press and hold the SHIFT key and click the rows or cells.

razor
selection.cs
@(Html.EJS().TreeGrid("SelectionAPI").AllowPaging().DataSource((IEnumerable<object>)ViewBag.datasource)
      .AllowSelection().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("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(190).Add();
      })
      .ChildMapping("Children").SelectionSettings(select => select.Type(Syncfusion.EJ2.Grids.SelectionType.Multiple)).TreeColumnIndex(1).Render())
public ActionResult Index()
{
    var tree = TreeData.GetDefaultData();
    ViewBag.datasource = tree;
    return View();
}

Selection mode

The treegrid supports three types of selection mode that can be set by using the Mode property of SelectionSettings. They are:

  • Row: The Row value is set by default, and allows you to select only rows.
  • Cell: Allows you to select only cells.
  • Both: Allows you to select rows and cells at the same time.
razor
selection-mode.cs
@(Html.EJS().TreeGrid("SelectionAPI").AllowPaging().DataSource((IEnumerable<object>)ViewBag.datasource)
      .AllowSelection()
      .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("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(190).Add();
       })
       .ChildMapping("Children").SelectionSettings(select => select.Mode(Syncfusion.EJ2.Grids.SelectionMode.Both)).TreeColumnIndex(1).Render())
public ActionResult Index()
{
    var tree = TreeData.GetDefaultData();
    ViewBag.datasource = tree;
    return View();
}

Cell selection

Cell selection can be done through simple mouse down or arrow keys (up, down, left, and right).

The treegrid supports two types of cell selection mode that can be set by using the CellSelectionMode property of SelectionSettings. They are:

  • Flow: The Flow value is set by default. The range of cells are selected between the start index and end index that includes in between cells of rows.
  • Box: Range of cells are selected from the start and end column indexes that includes in between cells of rows within the range.
razor
cell-selection.cs
@(Html.EJS().TreeGrid("SelectionAPI").AllowPaging()
            .DataSource((IEnumerable<object>)ViewBag.datasource)
            .AllowSelection()
            .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("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(190).Add();
            })
            .ChildMapping("Children").SelectionSettings(select => select.CellSelectionMode(Syncfusion.EJ2.Grids.CellSelectionMode.Flow)
            .Mode(Syncfusion.EJ2.Grids.SelectionMode.Cell).Type(Syncfusion.EJ2.Grids.SelectionType.Multiple)).TreeColumnIndex(1).Render())
public IActionResult Index()
{
    var tree = TreeData.GetDefaultData();
    ViewBag.datasource = tree;
    return View();
}

Cell selection requires the Mode to be Cell or Both, and Type should be Multiple.

Checkbox selection

Checkbox selection provides an option to select multiple treegrid records with help of checkbox in each row.

To render the checkbox in each treegrid row, you need to use checkbox column with type as checkbox using the column Type property.

razor
checkbox.cs
@(Html.EJS().TreeGrid("CheckboxSelection")
     .AllowPaging()
     .DataSource((IEnumerable<object>)ViewBag.datasource)
     .AllowSelection()
     .Columns(col =>
      {
        col.Type("checkbox").Width("50").Add();
        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("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(190).Add();
      })
      .ChildMapping("Children").TreeColumnIndex(2).Render())
public IActionResult Index()
{
    var tree = TreeData.GetDefaultData();
    ViewBag.datasource = tree;
    return View();
}

By default, selection is allowed by clicking a treegrid row or checkbox in that row. To allow selection only through checkbox, you can set the CheckboxOnly property of SelectionSettings to true. Selection can be persisted in all the operations using the PersistSelection property of SelectionSettings. For persisting selection on the treegrid, any one of the columns should be defined as a primary key using the IsPrimaryKey property of Column.

Checkbox selection Mode

In checkbox selection, selection can also be done by clicking on rows. This selection provides two types of Checkbox Selection mode which can be set by using the following API, CheckboxMode in SelectionSettings. The modes are;

  • Default: This is the default value of the checkboxMode. In this mode, user can select multiple rows by clicking rows one by one.
  • ResetOnRowClick: In ResetOnRowClick mode, when user clicks on a row it will reset previously selected row. Also you can perform multiple-selection in this mode by press and hold CTRL key and click the desired rows. To select range of rows, press and hold the SHIFT key and click the rows.
razor
@(Html.EJS().TreeGrid("CheckboxSelection")
      .AllowPaging()
      .DataSource((IEnumerable<object>)ViewBag.datasource)
      .AllowSelection()
      .Columns(col =>
       {
        col.Type("checkbox").Width("50").Add();
        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("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(190).Add();
       })
       .ChildMapping("Children").SelectionSettings(select => select.CheckboxMode(Syncfusion.EJ2.Grids.CheckboxSelectionType.ResetOnRowClick))
       .TreeColumnIndex(2).Render())