Row Selection in ASP.NET Core Grid Component

21 Dec 20229 minutes to read

Select row at Initial rendering

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

<ejs-grid id="Grid" dataSource="@ViewBag.datasource" allowSelection="true" selectedRowIndex="1" allowPaging="true">
    <e-grid-selectionsettings type="Multiple" mode="Both"></e-grid-selectionsettings>
    <e-grid-pagesettings pageCount="2"></e-grid-pagesettings>
    <e-grid-columns>
        <e-grid-column field="Inventor" headerText="Inventor Name" width="140"></e-grid-column>
        <e-grid-column field="NumberofPatentFamilies" headerText="No of Patent Families" width="185" textAlign="Right"></e-grid-column>
        <e-grid-column field="Country" headerText="Country" width="120"></e-grid-column>
        <e-grid-column field="Active" headerText="Active" width="130"></e-grid-column>
    </e-grid-columns>
 </ejs-grid>
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.

<ejs-grid id="Grid" dataSource="@ViewBag.datasource" rowSelected="rowSelected" allowSelection="true" allowPaging="true">
    <e-grid-selectionsettings type="Multiple"></e-grid-selectionsettings>
    <e-grid-pagesettings pageCount="2"></e-grid-pagesettings>
    <e-grid-columns>
        <e-grid-column field="Inventor" headerText="Inventor Name" width="140"></e-grid-column>
        <e-grid-column field="NumberofPatentFamilies" headerText="No of Patent Families" width="185" textAlign="Right"></e-grid-column>
        <e-grid-column field="Country" headerText="Country" width="120"></e-grid-column>
        <e-grid-column field="Active" headerText="Active" width="130"></e-grid-column>
    </e-grid-columns>
 </ejs-grid>

 <script>

 function rowSelected(args){
    var selectedrowindex= this.getSelectedRowIndexes();  // get the selected row indexes.
    alert(selectedrowindex); // to alert the selected row indexes.
    var selectedrecords = this.getSelectedRecords();  // get the selected records.
 }
 </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 enableSimpleMultiRowSelection property of e-grid-selectionsettings tag helper.

<ejs-grid id="Grid" dataSource="@ViewBag.datasource" allowSelection="true" allowPaging="true">
    <e-grid-selectionsettings enableSimpleMultiRowSelection="true" type="Multiple"></e-grid-selectionsettings>
    <e-grid-pagesettings pageCount="2"></e-grid-pagesettings>
    <e-grid-columns>
        <e-grid-column field="Inventor" headerText="Inventor Name" width="140"></e-grid-column>
        <e-grid-column field="NumberofPatentFamilies" headerText="No of Patent Families" width="185" textAlign="Right"></e-grid-column>
        <e-grid-column field="Country" headerText="Country" width="120"></e-grid-column>
        <e-grid-column field="Active" headerText="Active" width="130"></e-grid-column>
    </e-grid-columns>
 </ejs-grid>
public IActionResult Index()
 {
    var orders = InventorDetails.GetAllRecords();
    ViewBag.datasource = orders;            
    return View();
 }

Toggle selection

The Toggle selection allows to perform selection and unselection of the particular row or cell or column. To enable toggle selection, set enableToggle property of e-grid-selectionsettings as true. If you click on the selected row or cell or column then it will be unselected and vice versa.

<ejs-grid id="Grid" dataSource="@ViewBag.datasource" allowSelection="true" allowPaging="true">
    <e-grid-selectionsettings type="Multiple" enableToggle="true"></e-grid-selectionsettings>
    <e-grid-pagesettings pageCount="2"></e-grid-pagesettings>
    <e-grid-columns>
        <e-grid-column field="Inventor" headerText="Inventor Name" width="140"></e-grid-column>
        <e-grid-column field="NumberofPatentFamilies" headerText="No of Patent Families" width="185" textAlign="Right"></e-grid-column>
        <e-grid-column field="Country" headerText="Country" width="120"></e-grid-column>
        <e-grid-column field="Active" headerText="Active" width="130"></e-grid-column>
    </e-grid-columns>
 </ejs-grid>
public IActionResult Index()
 {
   var orders = OrderDetails.GetAllRecords();
   ViewBag.datasource = orders;            
   return View();
 }

NOTE

If multi selection is enabled, then first click on any selected row (without pressing Ctrl key), it will clear the multi selection and in second click on the same row, it will be unselected.

Clear selection programmatically

You can clear the Grid selection programmatically by using the clearSelection method.

In the demo below, we initially selected the third row using SelectedRowIndex. You can clear this selection by calling the clearSelection method in the button click event.

<ejs-button id="Button" content="Clear Selection"  cssClass="e-flat"></ejs-button>

<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" allowSelection="true" selectedRowIndex="2" allowPaging="true">
    <e-grid-selectionsettings type="Multiple"></e-grid-selectionsettings>
    <e-grid-columns>
        <e-grid-column field="OrderID" headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="120"></e-grid-column>
        <e-grid-column field="CustomerID" headerText="Customer Name" width="150"></e-grid-column>
        <e-grid-column field="Freight" headerText="Freight" textAlign="Right" format="C2" width="120"></e-grid-column>
        <e-grid-column field="ShipName" headerText="Ship Name" width="200"></e-grid-column>
    </e-grid-columns>
</ejs-grid>

<script>
    document.getElementById('Button').onclick = () => {
        var grid = document.getElementById("Grid").ej2_instances[0];
        grid.clearSelection();
    }
</script>
public IActionResult Index()
 {
    var orders = InventorDetails.GetAllRecords();
    ViewBag.Datasource = orders;            
    return View();
 }

Get selected records on various pages

Enabling the SelectionSettings.PersistSelection property will persist the selection in all Grid operations.

So the selection will be maintained on every page even after navigating to another page.

You can get the selected records using the getSelectedRecords method.

<ejs-button id="Button" content="Clear Selection"  cssClass="e-flat"></ejs-button>

<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" allowSelection="true" allowPaging="true">
    <e-grid-selectionsettings type="Multiple" persistSelection="true"></e-grid-selectionsettings>
    <e-grid-pagesettings pageSize="5"></e-grid-pagesettings>
    <e-grid-columns>
        <e-grid-column type="checkbox" headerText="Ship Name" width="200"></e-grid-column>
        <e-grid-column field="OrderID" headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="120"></e-grid-column>
        <e-grid-column field="CustomerID" headerText="Customer Name" width="150"></e-grid-column>
        <e-grid-column field="Freight" headerText="Freight" textAlign="Right" format="C2" width="120"></e-grid-column>
        <e-grid-column field="ShipName" headerText="Ship Name" width="200"></e-grid-column>
    </e-grid-columns>
</ejs-grid>

<script>
    document.getElementById('Button').onclick = () => {
        var grid = document.getElementById("Grid").ej2_instances[0];
        var selectedrecords = grid.getSelectedRecords();
        var selectedRecordsCount = selectedrecords.length;
        alert(selectedRecordsCount);
    }
</script>
public IActionResult Index()
 {
    var orders = InventorDetails.GetAllRecords();
    ViewBag.datasource = orders;            
    return View();
 }