Checkbox Selection in ASP.NET CORE Grid Component
21 Dec 20225 minutes to read
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 of e-grid-column tag helper.
<ejs-grid id="Grid" dataSource="@ViewBag.datasource" 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 type="checkbox" width="50"></e-grid-column>
<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();
}
NOTE
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
checkboxOnly
property of e-grid-selectionsettings as true.
Selection can be persisted in all the operations using thepersistSelection
property of e-grid-selectionsettings.
For persisting selection on the grid, any one of the columns should be defined as a primary key using theisPrimaryKey
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, 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.
<ejs-grid id="Grid" dataSource="@ViewBag.datasource" allowSelection="true" allowPaging="true">
<e-grid-selectionsettings checkboxMode="ResetOnRowClick" type="Multiple"></e-grid-selectionsettings>
<e-grid-pagesettings pageCount="2"></e-grid-pagesettings>
<e-grid-columns>
<e-grid-column type="checkbox" width="50"></e-grid-column>
<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();
}
Prevent specific rows from being selected in checkbox selection
You can prevent specific rows from being selected in the checkbox selection mode by hiding the checkboxes using the rowDataBound event. You achieve this by setting the isSelectable
argument as false in the rowDataBound
event based on certain conditions as per the needs of the application.
In the following sample, the selection of specific rows has been prevented based on the isSelectable
argument in the rowDataBound
event.
<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" allowFiltering="true" rowDataBound="rowDataBound" toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Update", "Cancel", "Search" })">
<e-grid-selectionSettings persistSelection="true"></e-grid-selectionSettings>
<e-grid-filterSettings type="CheckBox"></e-grid-filterSettings>
<e-grid-pagesettings pageSize="20"></e-grid-pagesettings>
<e-grid-editSettings allowAdding="true" allowDeleting="true" allowEditing="true"></e-grid-editSettings>
<e-grid-columns>
<e-grid-column type="checkbox" width="120"></e-grid-column>
<e-grid-column field="List" headerText="List" width="120"></e-grid-column>
<e-grid-column field="OrderID" headerText="OrderID" isPrimaryKey="true" textAlign="Right" width="150"></e-grid-column>
<e-grid-column field="CustomerID" headerText="CustomerID" width="150"></e-grid-column>
<e-grid-column field="EmployeeID" headerText="EmployeeID" width="150"></e-grid-column>
<e-grid-column field="ShipCity" headerText="ShipCity" width="150"></e-grid-column>
</e-grid-columns>
</ejs-grid>
<script>
function rowDataBound(args) {
args.isSelectable = args.data.List % 5 === 0;
}
</script>
public IActionResult Index()
{
ViewBag.DataSource = OrderDetails.GetAllRecords();
return View();
}