Example of Batch Editing in ASP.NET MVC Data Grid Control
This sample demonstrates the batch editing capabilities of the Grid, allowing users to perform multiple CRUD operations and save them to the data source in a single action. It showcases efficient data editing with bulk update and undo/redo support.
@Html.UiGrid(new UiGridOptions
{
Id = "BatchEdit",
Data = ViewData["dataSource"],
Height = "400px",
AllowPaging = true,
PageSize = 12,
AllowSorting = true,
AllowFiltering = true,
FilterType = UiGridFilterType.CheckBox,
SelectionSettings = new UiSelectionSettings { Mode = UiSelectionMode.Cell },
EditSettings = new UiEditSettings { AllowAdding = true, AllowEditing = true, AllowDeleting = true, Mode = UiEditMode.Batch, EnableUndoRedo = true },
Toolbar = new List<string> { "Add", "Delete", "Update", "Cancel", "Undo", "Redo" },
OnCellEdit = "cellEdit",
OnBeforeBatchSave = "beforeBatchSave",
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "ID", HeaderText = "ID", Width = 120, TextAlign = UiTextAlign.Right, IsPrimaryKey = true, ValidationRules = new UiValidationRules { Required = true }, ClipMode = UiClipMode.EllipsisWithTooltip },
new UiGridColumn { Field = "Product", HeaderText = "Product Name", Width = 180, Template = "#categoryDetail", EditType = UiEditType.DropDownEdit, EditItems = (List<string>)ViewData["products"], DefaultValue = "MacBook Pro" },
new UiGridColumn { Field = "VendorA", HeaderText = "Vendor A (units)", Width = 160, TextAlign = UiTextAlign.Right, Type = UiColumnType.Number, EditType = UiEditType.NumericEdit, ClipMode = UiClipMode.EllipsisWithTooltip },
new UiGridColumn { Field = "VendorB", HeaderText = "Vendor B (units)", Width = 160, TextAlign = UiTextAlign.Right, Type = UiColumnType.Number, EditType = UiEditType.NumericEdit, ClipMode = UiClipMode.EllipsisWithTooltip },
new UiGridColumn { Field = "VendorC", HeaderText = "Vendor C (units)", Width = 160, TextAlign = UiTextAlign.Right, Type = UiColumnType.Number, EditType = UiEditType.NumericEdit, ClipMode = UiClipMode.EllipsisWithTooltip },
new UiGridColumn { Field = "VendorD", HeaderText = "Vendor D (units)", Width = 160, TextAlign = UiTextAlign.Right, Type = UiColumnType.Number, EditType = UiEditType.NumericEdit, ClipMode = UiClipMode.EllipsisWithTooltip },
new UiGridColumn { Field = "UnitPrice", HeaderText = "Price (per unit)", Width = 160, TextAlign = UiTextAlign.Right, Type = UiColumnType.Number, EditType = UiEditType.NumericEdit, ClipMode = UiClipMode.EllipsisWithTooltip, Format = "C2", ValidationRules = new UiValidationRules { Required = true, Min = 1 } }
},
Aggregates = new List<UiGridAggregateRow>
{
new UiGridAggregateRow
{
Columns = new List<UiGridAggregateColumn>
{
new UiGridAggregateColumn { Field = "VendorA", Type = UiAggregateType.Sum, Format = "N", FooterTemplate = "Total: ${Sum}" },
new UiGridAggregateColumn { Field = "VendorB", Type = UiAggregateType.Sum, Format = "N", FooterTemplate = "Total: ${Sum}" },
new UiGridAggregateColumn { Field = "VendorC", Type = UiAggregateType.Sum, Format = "N", FooterTemplate = "Total: ${Sum}" },
new UiGridAggregateColumn { Field = "VendorD", Type = UiAggregateType.Sum, Format = "N", FooterTemplate = "Total: ${Sum}" }
}
}
}
})
<style>
#BatchEdit .e-category-badge { padding: 2px 6px; border-radius: 4px; display: inline-block; text-align: center; min-width: 80px; font-size: 11px; font-weight: 500; color: white; margin-top: 6px; background-color: #6b7280; }
#BatchEdit .e-category-badge[data-category="IT Asset"] { background-color: #4f46e5; }
#BatchEdit .e-category-badge[data-category="IT Infrastructure"] { background-color: #10b981; }
#BatchEdit .e-category-badge[data-category="Admin"] { background-color: #facc15; color: #1f2937; }
#BatchEdit .e-category-badge[data-category="Security"] { background-color: #ef4444; }
#BatchEdit .e-category-badge[data-category="Facilities"] { background-color: #64748b; }
#BatchEdit .e-category-badge[data-category="Finance"] { background-color: #8b5cf6; }
#BatchEdit .e-category-badge[data-category="Sales"] { background-color: #ec4899; }
#BatchEdit .e-category-badge[data-category="Marketing"] { background-color: #f97316; }
#BatchEdit .e-category-badge[data-category="Training"] { background-color: #06b6d4; }
</style>
<template id="categoryDetail">
<div>
<div style="font-weight: 600; font-size: 14px">${Product}</div>
<span class="e-category-badge" data-category="${Category}">${Category}</span>
</div>
</template>
@Html.UiGrid(new UiGridOptions
{
Id = "BatchEdit",
Data = ViewData["dataSource"],
Height = "400px",
AllowPaging = true,
PageSize = 12,
AllowSorting = true,
AllowFiltering = true,
FilterType = UiGridFilterType.CheckBox,
SelectionSettings = new UiSelectionSettings { Mode = UiSelectionMode.Cell },
EditSettings = new UiEditSettings { AllowAdding = true, AllowEditing = true, AllowDeleting = true, Mode = UiEditMode.Batch, EnableUndoRedo = true },
Toolbar = new List<string> { "Add", "Delete", "Update", "Cancel", "Undo", "Redo" },
OnCellEdit = "cellEdit",
OnBeforeBatchSave = "beforeBatchSave",
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "ID", HeaderText = "ID", Width = 120, TextAlign = UiTextAlign.Right, IsPrimaryKey = true, ValidationRules = new UiValidationRules { Required = true }, ClipMode = UiClipMode.EllipsisWithTooltip },
new UiGridColumn { Field = "Product", HeaderText = "Product Name", Width = 180, Template = "#categoryDetail", EditType = UiEditType.DropDownEdit, EditItems = (List<string>)ViewData["products"], DefaultValue = "MacBook Pro" },
new UiGridColumn { Field = "VendorA", HeaderText = "Vendor A (units)", Width = 160, TextAlign = UiTextAlign.Right, Type = UiColumnType.Number, EditType = UiEditType.NumericEdit, ClipMode = UiClipMode.EllipsisWithTooltip },
new UiGridColumn { Field = "VendorB", HeaderText = "Vendor B (units)", Width = 160, TextAlign = UiTextAlign.Right, Type = UiColumnType.Number, EditType = UiEditType.NumericEdit, ClipMode = UiClipMode.EllipsisWithTooltip },
new UiGridColumn { Field = "VendorC", HeaderText = "Vendor C (units)", Width = 160, TextAlign = UiTextAlign.Right, Type = UiColumnType.Number, EditType = UiEditType.NumericEdit, ClipMode = UiClipMode.EllipsisWithTooltip },
new UiGridColumn { Field = "VendorD", HeaderText = "Vendor D (units)", Width = 160, TextAlign = UiTextAlign.Right, Type = UiColumnType.Number, EditType = UiEditType.NumericEdit, ClipMode = UiClipMode.EllipsisWithTooltip },
new UiGridColumn { Field = "UnitPrice", HeaderText = "Price (per unit)", Width = 160, TextAlign = UiTextAlign.Right, Type = UiColumnType.Number, EditType = UiEditType.NumericEdit, ClipMode = UiClipMode.EllipsisWithTooltip, Format = "C2", ValidationRules = new UiValidationRules { Required = true, Min = 1 } }
},
Aggregates = new List<UiGridAggregateRow>
{
new UiGridAggregateRow
{
Columns = new List<UiGridAggregateColumn>
{
new UiGridAggregateColumn { Field = "VendorA", Type = UiAggregateType.Sum, Format = "N", FooterTemplate = "Total: ${Sum}" },
new UiGridAggregateColumn { Field = "VendorB", Type = UiAggregateType.Sum, Format = "N", FooterTemplate = "Total: ${Sum}" },
new UiGridAggregateColumn { Field = "VendorC", Type = UiAggregateType.Sum, Format = "N", FooterTemplate = "Total: ${Sum}" },
new UiGridAggregateColumn { Field = "VendorD", Type = UiAggregateType.Sum, Format = "N", FooterTemplate = "Total: ${Sum}" }
}
}
}
})
<script>
// The products and their categories, to give a new row its category when the changes are saved.
var inventory = @Html.Raw(UiJson.Serialize(ViewData["dataSource"]));
function getCategoryFromProduct(productName) {
var item = inventory.filter(function (data) { return data.Product === productName; })[0];
return item ? item.Category : '';
}
// The product of a saved row cannot be changed; a new row picks one.
function cellEdit(args) {
if (args.type === 'edit' && args.columnName === 'Product') {
args.cancel = true;
}
}
function beforeBatchSave(args) {
args.batchChanges.addedRecords.forEach(function (row) {
row.Category = getCategoryFromProduct(row.Product);
});
}
</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/BatchEditing.cshtml; it draws what ViewData holds.
public ActionResult BatchEditing()
{
var order = InventoryStoreData.GetAllRecords();
ViewData["dataSource"] = order;
ViewData["products"] = order.Select(o => o.Product).Distinct().ToList();
return View();
}
public class InventoryStoreData
{
public string ID { get; set; }
public string Product { get; set; }
public string Category { get; set; }
public decimal UnitPrice { get; set; }
public int VendorA { get; set; }
public int VendorB { get; set; }
public int VendorC { get; set; }
public int VendorD { get; set; }
public static List<InventoryStoreData> GetAllRecords()
{
return new List<InventoryStoreData>
{
new InventoryStoreData { ID = "ID-1001", Product = "MacBook Pro", Category = "IT Asset", UnitPrice = 1200, VendorA = 55, VendorB = 40, VendorC = 60, VendorD = 35 },
new InventoryStoreData { ID = "ID-1002", Product = "Wireless Mouse", Category = "IT Asset", UnitPrice = 25, VendorA = 120, VendorB = 95, VendorC = 110, VendorD = 80 },
new InventoryStoreData { ID = "ID-1003", Product = "4K Monitor", Category = "IT Asset", UnitPrice = 400, VendorA = 45, VendorB = 50, VendorC = 38, VendorD = 42 },
// ... 27 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. *@
Batch mode is enabled by setting EditSettings.Mode to Batch. The Grid also supports undo and redo functionality in this mode, enabling users to reverse or reapply changes during an editing session. This feature is enabled by setting EditSettings.EnableUndoRedo to true and include the Undo and Redo items in the Toolbar.
With Batch editing, bulk data changes can be made efficiently. Editing begins by double-clicking a cell and modifying its value. The edited cell is highlighted when moving to another cell, making changes easy to track. All modifications remain local until they are explicitly saved. The modified records are saved to the data source by clicking the toolbar's "Update" button, which performs a bulk save operation.