Example of Cell Editing in ASP.NET MVC Tree Grid Control
This sample demonstrates cell editing in the TreeGrid, allowing users to quickly update individual field values directly within the data view.
@Html.UiTreeGrid(new UiTreeGridOptions
{
Id = "TreeGrid",
OnQueryCellInfo = "queryCellInfo",
Data = ViewData["datasource"],
IdMapping = "productId",
ParentIdMapping = "parentId",
TreeColumnIndex = 1,
Height = "500px",
AllowPaging = true,
PageSize = 12,
EditSettings = new UiEditSettings { AllowAdding = true, AllowEditing = true, AllowDeleting = true, Mode = UiEditMode.Cell },
Selection = UiGridSelection.Single,
Toolbar = new List<string> { "Add", "Delete", "Update", "Cancel" },
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "productId", HeaderText = "Product ID", Type = UiColumnType.Number, IsPrimaryKey = true, TextAlign = UiTextAlign.Right, EditType = UiEditType.NumericEdit, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "productName", HeaderText = "Product Name", ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "supplier", HeaderText = "Supplier" },
new UiGridColumn { Field = "stockQty", HeaderText = "Stock Qty", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, EditType = UiEditType.NumericEdit, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "unitPrice", HeaderText = "Unit Price (₹)", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, Format = "C2", EditType = UiEditType.NumericEdit, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "status", HeaderText = "Status", EditType = UiEditType.DropDownEdit, EditItems = new List<string> { "Active", "Low Stock", "Pending Restock", "Out of Stock", "Discontinued" }, ValidationRules = new UiValidationRules { Required = true } }
}
})
<style>
.status-badge {
display: inline-block;
padding: 4px 10px;
border-radius: 12px;
font-size: 12px;
font-weight: 600;
text-align: center;
}
.status-active {
color: #166534;
background-color: #dcfce7;
}
.status-low-stock {
color: #92400e;
background-color: #fef3c7;
}
.status-pending {
color: #1d4ed8;
background-color: #dbeafe;
}
.status-out-stock {
color: #991b1b;
background-color: #fee2e2;
}
.status-discontinued {
color: #374151;
background-color: #e5e7eb;
}
</style>
@Html.UiTreeGrid(new UiTreeGridOptions
{
Id = "TreeGrid",
OnQueryCellInfo = "queryCellInfo",
Data = ViewData["datasource"],
IdMapping = "productId",
ParentIdMapping = "parentId",
TreeColumnIndex = 1,
Height = "500px",
AllowPaging = true,
PageSize = 12,
EditSettings = new UiEditSettings { AllowAdding = true, AllowEditing = true, AllowDeleting = true, Mode = UiEditMode.Cell },
Selection = UiGridSelection.Single,
Toolbar = new List<string> { "Add", "Delete", "Update", "Cancel" },
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "productId", HeaderText = "Product ID", Type = UiColumnType.Number, IsPrimaryKey = true, TextAlign = UiTextAlign.Right, EditType = UiEditType.NumericEdit, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "productName", HeaderText = "Product Name", ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "supplier", HeaderText = "Supplier" },
new UiGridColumn { Field = "stockQty", HeaderText = "Stock Qty", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, EditType = UiEditType.NumericEdit, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "unitPrice", HeaderText = "Unit Price (₹)", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, Format = "C2", EditType = UiEditType.NumericEdit, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "status", HeaderText = "Status", EditType = UiEditType.DropDownEdit, EditItems = new List<string> { "Active", "Low Stock", "Pending Restock", "Out of Stock", "Discontinued" }, ValidationRules = new UiValidationRules { Required = true } }
}
})
<script>
var statusClasses = { 'Active': 'status-active', 'Low Stock': 'status-low-stock', 'Pending Restock': 'status-pending', 'Out of Stock': 'status-out-stock' };
// Syncfusion's statusTemplate: the status as a badge whose colour is its value's.
function queryCellInfo(args) {
if (args.column === 'status') {
var badge = document.createElement('span');
badge.className = 'status-badge ' + (statusClasses[args.data.status] || 'status-discontinued');
badge.textContent = args.data.status;
args.content = badge;
}
}
</script>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class TreeGridController : Controller
{
// The page is Views/TreeGrid/CellEditing.cshtml; it draws what ViewData holds.
public ActionResult CellEditing()
{
var treeData = RetailInventoryData.GetRetailInventoryData();
ViewData["datasource"] = treeData;
return View();
}
public class RetailInventoryData
{
public int productId { get; set; }
public int? parentId { get; set; }
public string productName { get; set; }
public string supplier { get; set; }
public int? stockQty { get; set; }
public int? reorderLevel { get; set; }
public double? unitPrice { get; set; }
public string status { get; set; }
public static List<RetailInventoryData> GetRetailInventoryData()
{
List<RetailInventoryData> data = new List<RetailInventoryData>();
// Beverages
data.Add(new RetailInventoryData() { productId = 1001, productName = "Beverages", supplier = "Prime Distributors", stockQty = null, reorderLevel = null, unitPrice = null, status = "Active", parentId = null });
data.Add(new RetailInventoryData() { productId = 1002, productName = "Cola Drink 500ml", supplier = "Metro Distribution", stockQty = 250, reorderLevel = 50, unitPrice = 40, status = "Active", parentId = 1001 });
data.Add(new RetailInventoryData() { productId = 1003, productName = "Lemon Soda 500ml", supplier = "City Supply Partners", stockQty = 180, reorderLevel = 40, unitPrice = 38, status = "Active", parentId = 1001 });
data.Add(new RetailInventoryData() { productId = 1004, productName = "Orange Soda 500ml", supplier = "West End Traders", stockQty = 55, reorderLevel = 60, unitPrice = 40, status = "Low Stock", parentId = 1001 });
data.Add(new RetailInventoryData() { productId = 1005, productName = "Energy Drink 250ml", supplier = "Global Retail Supplies", stockQty = 15, reorderLevel = 30, unitPrice = 125, status = "Pending Restock", parentId = 1001 });
data.Add(new RetailInventoryData() { productId = 1006, productName = "Orange Juice 1L", supplier = "North Region Suppliers", stockQty = 0, reorderLevel = 20, unitPrice = 110, status = "Out of Stock", parentId = 1001 });
data.Add(new RetailInventoryData() { productId = 1007, productName = "Mixed Fruit Juice 1L", supplier = "Universal Traders", stockQty = 140, reorderLevel = 30, unitPrice = 105, status = "Active", parentId = 1001 });
// Dairy Products
data.Add(new RetailInventoryData() { productId = 1008, productName = "Dairy Products", supplier = "Central Wholesale", stockQty = null, reorderLevel = null, unitPrice = null, status = "Active", parentId = null });
data.Add(new RetailInventoryData() { productId = 1009, productName = "Fresh Milk 1L", supplier = "South Region Suppliers", stockQty = 320, reorderLevel = 75, unitPrice = 68, status = "Active", parentId = 1008 });
data.Add(new RetailInventoryData() { productId = 1010, productName = "Low Fat Milk 1L", supplier = "Prime Distributors", stockQty = 45, reorderLevel = 60, unitPrice = 72, status = "Low Stock", parentId = 1008 });
data.Add(new RetailInventoryData() { productId = 1011, productName = "Butter 100g", supplier = "National Supply Chain", stockQty = 155, reorderLevel = 30, unitPrice = 58, status = "Active", parentId = 1008 });
data.Add(new RetailInventoryData() { productId = 1012, productName = "Cheese Slices", supplier = "Metro Distribution", stockQty = 120, reorderLevel = 25, unitPrice = 145, status = "Active", parentId = 1008 });
data.Add(new RetailInventoryData() { productId = 1013, productName = "Probiotic Drink", supplier = "Global Retail Supplies", stockQty = 0, reorderLevel = 15, unitPrice = 95, status = "Out of Stock", parentId = 1008 });
data.Add(new RetailInventoryData() { productId = 1014, productName = "Curd 400g", supplier = "City Supply Partners", stockQty = 170, reorderLevel = 35, unitPrice = 42, status = "Active", parentId = 1008 });
// Snacks
data.Add(new RetailInventoryData() { productId = 1015, productName = "Snacks", supplier = "Metro Distribution", stockQty = null, reorderLevel = null, unitPrice = null, status = "Active", parentId = null });
data.Add(new RetailInventoryData() { productId = 1016, productName = "Potato Chips Classic", supplier = "West End Traders", stockQty = 350, reorderLevel = 80, unitPrice = 20, status = "Active", parentId = 1015 });
data.Add(new RetailInventoryData() { productId = 1017, productName = "Potato Chips Masala", supplier = "North Region Suppliers", stockQty = 310, reorderLevel = 70, unitPrice = 20, status = "Active", parentId = 1015 });
data.Add(new RetailInventoryData() { productId = 1018, productName = "Triangle Corn Chips", supplier = "Universal Traders", stockQty = 220, reorderLevel = 50, unitPrice = 25, status = "Active", parentId = 1015 });
data.Add(new RetailInventoryData() { productId = 1019, productName = "Nacho Chips", supplier = "Prime Distributors", stockQty = 18, reorderLevel = 30, unitPrice = 55, status = "Pending Restock", parentId = 1015 });
data.Add(new RetailInventoryData() { productId = 1020, productName = "Spicy Mixture", supplier = "South Region Suppliers", stockQty = 185, reorderLevel = 40, unitPrice = 48, status = "Active", parentId = 1015 });
data.Add(new RetailInventoryData() { productId = 1021, productName = "Crunchy Corn Snacks", supplier = "Central Wholesale", stockQty = 45, reorderLevel = 60, unitPrice = 20, status = "Low Stock", parentId = 1015 });
// Personal Care
data.Add(new RetailInventoryData() { productId = 1022, productName = "Personal Care", supplier = "Universal Traders", stockQty = null, reorderLevel = null, unitPrice = null, status = "Active", parentId = null });
data.Add(new RetailInventoryData() { productId = 1023, productName = "Moisturizing Soap", supplier = "Global Retail Supplies", stockQty = 140, reorderLevel = 30, unitPrice = 48, status = "Active", parentId = 1022 });
data.Add(new RetailInventoryData() { productId = 1024, productName = "Beauty Soap", supplier = "Metro Distribution", stockQty = 190, reorderLevel = 40, unitPrice = 38, status = "Active", parentId = 1022 });
data.Add(new RetailInventoryData() { productId = 1025, productName = "Mint Toothpaste", supplier = "City Supply Partners", stockQty = 210, reorderLevel = 50, unitPrice = 95, status = "Active", parentId = 1022 });
data.Add(new RetailInventoryData() { productId = 1026, productName = "Herbal Toothpaste", supplier = "West End Traders", stockQty = 35, reorderLevel = 50, unitPrice = 88, status = "Low Stock", parentId = 1022 });
data.Add(new RetailInventoryData() { productId = 1027, productName = "Body Lotion", supplier = "National Supply Chain", stockQty = 0, reorderLevel = 0, unitPrice = 225, status = "Discontinued", parentId = 1022 });
data.Add(new RetailInventoryData() { productId = 1028, productName = "Daily Care Shampoo", supplier = "South Region Suppliers", stockQty = 130, reorderLevel = 30, unitPrice = 78, status = "Active", parentId = 1022 });
// Household Essentials
data.Add(new RetailInventoryData() { productId = 1029, productName = "Household Essentials", supplier = "National Supply Chain", stockQty = null, reorderLevel = null, unitPrice = null, status = "Active", parentId = null });
data.Add(new RetailInventoryData() { productId = 1030, productName = "Laundry Detergent", supplier = "Prime Distributors", stockQty = 145, reorderLevel = 35, unitPrice = 265, status = "Active", parentId = 1029 });
data.Add(new RetailInventoryData() { productId = 1031, productName = "Premium Detergent", supplier = "Universal Traders", stockQty = 25, reorderLevel = 30, unitPrice = 280, status = "Low Stock", parentId = 1029 });
data.Add(new RetailInventoryData() { productId = 1032, productName = "Dishwash Gel", supplier = "North Region Suppliers", stockQty = 180, reorderLevel = 40, unitPrice = 75, status = "Active", parentId = 1029 });
data.Add(new RetailInventoryData() { productId = 1033, productName = "Toilet Cleaner", supplier = "City Supply Partners", stockQty = 12, reorderLevel = 20, unitPrice = 115, status = "Pending Restock", parentId = 1029 });
data.Add(new RetailInventoryData() { productId = 1034, productName = "Floor Cleaner", supplier = "West End Traders", stockQty = 110, reorderLevel = 25, unitPrice = 165, status = "Active", parentId = 1029 });
data.Add(new RetailInventoryData() { productId = 1035, productName = "Glass Cleaner", supplier = "Global Retail Supplies", stockQty = 0, reorderLevel = 10, unitPrice = 95, status = "Out of Stock", parentId = 1029 });
return data;
}
}
}
}
@* 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>