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 Grid, set the AllowSelection to false.

The grid supports two types of selection that can be set by using the SelectionSettings.Type. 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().Grid("SelectionAPI").DataSource((IEnumerable<object>)ViewBag.dataSource).AllowSelection().Columns(col =>
{
    col.Field("Inventor").HeaderText("Inventor Name").Width("180").Add();
    col.Field("NumberofPatentFamilies").HeaderText("No of Patent Families").Width("195").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("Country").HeaderText("Country").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("Active").HeaderText("Active").Width("130").Add();

}).AllowPaging().SelectionSettings(select => select.Type(Syncfusion.EJ2.Grids.SelectionType.Multiple)).PageSettings(page => page.PageCount(2)).Render()
public IActionResult Index()
        {
            var orders=OrderDetails.GetAllRecords();
            ViewBag.datasource = orders;            
            return View();
        }

Selection mode

The grid supports three types of selection mode that can be set by using the SelectionSettings.Mode. 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().Grid("SelectionAPI").DataSource((IEnumerable<object>)ViewBag.dataSource).AllowSelection().Columns(col =>
{
    col.Field("Inventor").HeaderText("Inventor Name").Width("180").Add();
    col.Field("NumberofPatentFamilies").HeaderText("No of Patent Families").Width("195").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("Country").HeaderText("Country").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("Active").HeaderText("Active").Width("130").Add();

}).AllowPaging().SelectionSettings(select => select.Mode(Syncfusion.EJ2.Grids.SelectionMode.Both)).PageSettings(page => page.PageCount(2)).Render()
public IActionResult Index()
        {
            var orders=OrderDetails.GetAllRecords();
            ViewBag.datasource = orders;            
            return View();
        }

Cell selection

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

The grid supports two types of cell selection mode that can be set by using the SelectionSettings.CellSelectionMode. 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().Grid("Grid").DataSource((IEnumerable<object>)ViewBag.dataSource).AllowSelection().Columns(col =>
{
    col.Field("Inventor").HeaderText("Inventor Name").Width("180").Add();
    col.Field("NumberofPatentFamilies").HeaderText("No of Patent Families").Width("195").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("Country").HeaderText("Country").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("Active").HeaderText("Active").Width("130").Add();

}).AllowPaging().SelectionSettings(select => select.CellSelectionMode(Syncfusion.EJ2.Grids.CellSelectionMode.Flow).Mode(Syncfusion.EJ2.Grids.SelectionMode.Cell).Type(Syncfusion.EJ2.Grids.SelectionType.Multiple)).PageSettings(page => page.PageCount(2)).Render()
public IActionResult Index()
        {
            var orders = InventorDetails.GetAllRecords();
            ViewBag.datasource = orders;            
            return View();
        }

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

Checkbox selection

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

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

razor
checkbox.cs
@Html.EJS().Grid("CheckboxSelection").DataSource((IEnumerable<object>)ViewBag.dataSource).Columns(col =>
{
    col.Type("checkbox").Width("50").Add();
    col.Field("Inventor").HeaderText("Inventor Name").Width("180").Add();
    col.Field("NumberofPatentFamilies").HeaderText("No of Patent Families").Width("195").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("Country").HeaderText("Country").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("Active").HeaderText("Active").Width("130").Add();
}).AllowPaging().PageSettings(page => page.PageCount(2)).Render()
public IActionResult Index()
        {
            var orders = InventorDetails.GetAllRecords();
            ViewBag.datasource = orders;            
            return View();
        }

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

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, selectionSettings.checkboxMode. 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
windows.cs
@Html.EJS().Grid("SelectionAPI").DataSource((IEnumerable<object>)ViewBag.dataSource).AllowSelection().Columns(col =>
{
    col.Type("checkbox").Width("50").Add();
    col.Field("Inventor").HeaderText("Inventor Name").Width("180").Add();
    col.Field("NumberofPatentFamilies").HeaderText("No of Patent Families").Width("195").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("Country").HeaderText("Country").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("Active").HeaderText("Active").Width("130").Add();

}).AllowPaging().SelectionSettings(select => select.checkboxMode(Syncfusion.EJ2.Grids.CheckboxSelectionType.ResetOnRowClick).Type(Syncfusion.EJ2.Grids.SelectionType.Multiple)).Render()
public IActionResult Index()
        {
            var orders = InventorDetails.GetAllRecords();
            ViewBag.datasource = orders;            
            return View();
        }

Select row at Initial rendering

To select a row at initial rendering, set the SelectedRowIndex value.

razor
select-row.cs
@Html.EJS().Grid("SelectionAPI").DataSource((IEnumerable<object>)ViewBag.dataSource).AllowSelection().Columns(col =>
{
    col.Field("Inventor").HeaderText("Inventor Name").Width("180").Add();
    col.Field("NumberofPatentFamilies").HeaderText("No of Patent Families").Width("195").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("Country").HeaderText("Country").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("Active").HeaderText("Active").Width("130").Add();

}).AllowPaging().SelectedRowIndex(2).SelectionSettings(select => select.Type(Syncfusion.EJ2.Grids.SelectionType.Multiple).Mode(Syncfusion.EJ2.Grids.SelectionMode.Both)).PageSettings(page => page.PageCount(2)).Render()
public IActionResult Index()
        {
            var orders = InventorDetails.GetAllRecords();
            ViewBag.datasource = orders;            
            return View();
        }

Get selected row indexes

You can get the selected row indexes by using the getSelectedRowIndexes method.

razor
selected-row-index.cs
@Html.EJS().Grid("SelectionAPI").DataSource((IEnumerable<object>)ViewBag.dataSource).RowSelected("rowSelected").AllowSelection().Columns(col =>
 {
     col.Field("Inventor").HeaderText("Inventor Name").Width("180").Add();
     col.Field("NumberofPatentFamilies").HeaderText("No of Patent Families").Width("195").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
     col.Field("Country").HeaderText("Country").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
     col.Field("Active").HeaderText("Active").Width("130").Add();

 }).AllowPaging().SelectionSettings(select => select.Type(Syncfusion.EJ2.Grids.SelectionType.Multiple)).PageSettings(page => page.PageCount(2)).Render()

    <script>
 function rowSelected(args){
    var grid = document.getElementById("SelectionAPI").ej2_instances[0];
    var selectedrowindex= grid.getSelectedRowIndexes();  // get the selected row indexes.
    alert(selectedrowindex); // to alert the selected row indexes.
    var selectedrecords = grid.getSelectedRecords();  // get the selected records.
 }
    </script>
public IActionResult Index()
        {
            var orders=OrderDetails.GetAllRecords();
            ViewBag.datasource = orders;            
            return View();
        }

Touch interaction

When you tap a grid row on touchscreen device, the tapped row is selected. It also shows a popup selection for multi-row selection. To select multiple rows or cells, tap the popupmselection and then tap the desired rows or cells.

Multi-selection requires the selection Type to be Multiple.

The following screenshot represents a grid touch selection in the device.

Touch interaction

Multiple Selection based on condition

You can select multiple grid rows based on condition by using the selectRows method.

In the following code, the rows which contains ShipCountry value as Brazil are selected at initial rendering.

razor
conditional-selection.cs
@Html.EJS().Grid("SelectionAPI").DataSource((IEnumerable<object>)ViewBag.dataSource).DataBound("dataBound").AllowSelection().Columns(col =>
 {
       col.Field("OrderID").HeaderText("Order ID").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
       col.Field("CustomerID").HeaderText("Customer Name").Width("150").Add();
       col.Field("Freight").HeaderText("Freight").Width("120").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
       col.Field("ShipCountry").HeaderText("Ship Country").Width("150").Add();

 }).AllowPaging().SelectionSettings(select => select.Type(Syncfusion.EJ2.Grids.SelectionType.Multiple)).Render()

    <script>
 function dataBound(args){
        var rowIndexes =[]; 
        var grid = document.getElementById("SelectionAPI").ej2_instances[0];         
        grid.dataSource.forEach((data,index)=>{
		if(data.ShipCountry === "Brazil"){
               rowIndexes.push(index);
           }
		});
        grid.selectionModule.selectRows(rowIndexes);
 }
    </script>
public IActionResult Index()
        {
            var orders=OrderDetails.GetAllRecords();
            ViewBag.datasource = orders;            
            return View();
        }

Simple Multiple Row selection

You can select multiple rows by clicking on rows one by one. This will not deselect the previously selected rows. To deselect the previously selected row, you can click on the selected row. You can enable this behavior by using selectionSettings.enableSimpleMultiRowSelection property.

razor
multiselection.cs
@Html.EJS().Grid("SelectionAPI").DataSource((IEnumerable<object>)ViewBag.dataSource).AllowSelection().Columns(col =>
{
    col.Field("Inventor").HeaderText("Inventor Name").Width("180").Add();
    col.Field("NumberofPatentFamilies").HeaderText("No of Patent Families").Width("195").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("Country").HeaderText("Country").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("Active").HeaderText("Active").Width("130").Add();

}).AllowPaging().SelectionSettings(select => select.EnableSimpleMultiRowSelection(true).Type(Syncfusion.EJ2.Grids.SelectionType.Multiple)).Render()
public IActionResult Index()
        {
            var orders = InventorDetails.GetAllRecords();
            ViewBag.datasource = orders;            
            return View();
        }