Example of Conditional Row Selection in ASP.NET MVC Data Grid Control
This sample demonstrates the conditional row selection of the Data Grid using checkbox selection. It allows end-users to select only specific rows based on certain conditions.
${Product}
${Status}
@Html.UiGrid(new UiGridOptions
{
Id = "ConditionalSelection",
Height = "400px",
Data = ViewData["dataSource"],
EnableVirtualization = true,
IsRowSelectable = "isRowSelectable",
AllowSorting = true,
AllowFiltering = true,
FilterType = UiGridFilterType.Excel,
EditSettings = new UiEditSettings { AllowEditing = true },
SelectionSettings = new UiSelectionSettings { PersistSelection = true, CheckboxOnly = true },
Toolbar = new List<string> { "Edit", "Update", "Cancel" },
Columns = new List<UiGridColumn>
{
new UiGridColumn { Width = 60, Type = UiColumnType.CheckBox, AllowEditing = false },
new UiGridColumn { Field = "OrderID", HeaderText = "Order ID", IsPrimaryKey = true, Width = 110, AllowEditing = false },
new UiGridColumn { Field = "CustomerName", HeaderText = "Customer Name", Width = 170, AllowEditing = false },
new UiGridColumn { Field = "Product", HeaderText = "Product", Width = 150, AllowEditing = false, Template = "#productTemplate" },
new UiGridColumn { Field = "Amount", HeaderText = "Amount", Type = UiColumnType.Number, Width = 110, AllowEditing = false, TextAlign = UiTextAlign.Right, Format = "C2" },
new UiGridColumn { Field = "OrderDate", HeaderText = "Order Date", Type = UiColumnType.Date, Width = 130, AllowEditing = false, TextAlign = UiTextAlign.Right, Format = "M/d/yyyy" },
new UiGridColumn { Field = "Status", HeaderText = "Status", Width = 150, Template = "#statusTemplate", EditType = UiEditType.DropDownEdit }
}
})
<style>
#ConditionalSelection .e-gridcontent .e-product-info,
#ConditionalSelection .e-gridcontent .e-status-info {
display: flex;
align-items: center;
}
#ConditionalSelection .e-gridcontent .e-product-info img,
#ConditionalSelection .e-gridcontent .e-status-info img {
margin-right: 5px;
}
#ConditionalSelection .e-gridcontent .e-product-info img {
width: 40px;
height: 40px;
border-radius: 20px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1), 0 1px 3px rgba(0, 0, 0, 0.08);
}
.material3-dark #ConditionalSelection .e-gridcontent .e-status-info img,
.bootstrap5\.3-dark #ConditionalSelection .e-gridcontent .e-status-info img,
.fluent2-dark #ConditionalSelection .e-gridcontent .e-status-info img,
.tailwind3-dark #ConditionalSelection .e-gridcontent .e-status-info img,
.fluent2-highcontrast #ConditionalSelection .e-gridcontent .e-status-info img,
.fluent-dark #ConditionalSelection .e-gridcontent .e-status-info img,
.highcontrast #ConditionalSelection .e-gridcontent .e-status-info img {
filter: brightness(0) invert(1);
}
.fluent2-highcontrast #ConditionalSelection .e-gridcontent .e-row:hover .e-status-info img,
.fluent2-highcontrast #ConditionalSelection .e-gridcontent .e-rowcell.e-selectionbackground .e-status-info img {
filter: none;
}
</style>
<template id="productTemplate"><div class="e-product-info">
<span>${Product}</span>
</div></template>
<template id="statusTemplate"><div class="e-status-info">
<span>${Status}</span>
</div></template>
@Html.UiGrid(new UiGridOptions
{
Id = "ConditionalSelection",
Height = "400px",
Data = ViewData["dataSource"],
EnableVirtualization = true,
IsRowSelectable = "isRowSelectable",
AllowSorting = true,
AllowFiltering = true,
FilterType = UiGridFilterType.Excel,
EditSettings = new UiEditSettings { AllowEditing = true },
SelectionSettings = new UiSelectionSettings { PersistSelection = true, CheckboxOnly = true },
Toolbar = new List<string> { "Edit", "Update", "Cancel" },
Columns = new List<UiGridColumn>
{
new UiGridColumn { Width = 60, Type = UiColumnType.CheckBox, AllowEditing = false },
new UiGridColumn { Field = "OrderID", HeaderText = "Order ID", IsPrimaryKey = true, Width = 110, AllowEditing = false },
new UiGridColumn { Field = "CustomerName", HeaderText = "Customer Name", Width = 170, AllowEditing = false },
new UiGridColumn { Field = "Product", HeaderText = "Product", Width = 150, AllowEditing = false, Template = "#productTemplate" },
new UiGridColumn { Field = "Amount", HeaderText = "Amount", Type = UiColumnType.Number, Width = 110, AllowEditing = false, TextAlign = UiTextAlign.Right, Format = "C2" },
new UiGridColumn { Field = "OrderDate", HeaderText = "Order Date", Type = UiColumnType.Date, Width = 130, AllowEditing = false, TextAlign = UiTextAlign.Right, Format = "M/d/yyyy" },
new UiGridColumn { Field = "Status", HeaderText = "Status", Width = 150, Template = "#statusTemplate", EditType = UiEditType.DropDownEdit }
}
})
<script>
function isRowSelectable(data) {
if (data.Status === "Canceled" || data.Status === "Delivered") {
return false;
}
return true;
}
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class GridController : Controller
{
// The page is Views/Grid/ConditionalSelection.cshtml; it draws what ViewData holds.
public ActionResult ConditionalSelection()
{
var order = OrdersTrackData.GetAllRecords();
ViewData["datasource"] = order;
return View();
}
public class OrdersTrackData
{
public string OrderID { get; set; }
public string CustomerName { get; set; }
public string Product { get; set; }
public string Status { get; set; }
public DateTime OrderDate { get; set; }
public double Amount { get; set; }
public static List<OrdersTrackData> GetAllRecords()
{
return new List<OrdersTrackData>
{
new OrdersTrackData { OrderID = "ORD1001", CustomerName = "Eve Green", Product = "Laptop", Status = "Shipped", OrderDate = DateTime.Parse("2025-04-25"), Amount = 148.14 },
new OrdersTrackData { OrderID = "ORD1002", CustomerName = "Jack Black", Product = "Bag", Status = "Canceled", OrderDate = DateTime.Parse("2025-01-17"), Amount = 39.50 },
new OrdersTrackData { OrderID = "ORD1003", CustomerName = "David Brown", Product = "Shoe", Status = "Pending", OrderDate = DateTime.Parse("2025-10-15"), Amount = 206.85 },
// ... 496 more rows
};
}
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
@* Grids with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@
In this demo, conditional row selection is implemented using the IsRowSelectable callback function. This callback function executes before the grid loads data, evaluates each row, and returns false for orders with Delivered or Canceled status.
Selection is persisted by enabling selectionSettings -> persistSelection. With this setting, selected rows remain checked across all operations. Persist selection requires at least one column to be defined as a primary key using the columns -> isPrimaryKey property.