Example of Cell Editing in ASP.NET MVC Data Grid Control
This sample demonstrates cell editing for quick and efficient data updates. It provides a seamless editing experience for modifying individual cell values within the Grid.
@Html.UiGrid(new UiGridOptions
{
Id = "CellEdit",
Height = "400px",
OnActionBegin = "actionBegin",
OnQueryCellInfo = "queryCellInfo",
Data = ViewData["dataSource"],
RowHeight = 40,
AllowSorting = true,
AllowFiltering = true,
AllowPaging = true,
PageSize = 12,
FilterType = UiGridFilterType.CheckBox,
EditSettings = new UiEditSettings { AllowAdding = true, AllowEditing = true, AllowDeleting = true, Mode = UiEditMode.Cell },
Toolbar = new List<string> { "Add", "Delete", "Update", "Cancel" },
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "ApptID", HeaderText = "Appointment ID", IsPrimaryKey = true, Width = 140, Visible = false },
new UiGridColumn { Field = "Patient", HeaderText = "Patient", Width = 150, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "Doctor", HeaderText = "Doctor", Width = 160, EditType = UiEditType.DropDownEdit, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "AppointmentTime", HeaderText = "Appointment Time", Type = UiColumnType.Date, Width = 200, Format = "M/d/yyyy hh:mm tt", EditType = UiEditType.DateTimePickerEdit },
new UiGridColumn { Field = "Type", HeaderText = "Type", Width = 150, EditType = UiEditType.DropDownEdit, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "Status", HeaderText = "Status", Width = 130, EditType = UiEditType.DropDownEdit, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "Room", HeaderText = "Room No", Width = 120, EditType = UiEditType.DropDownEdit },
new UiGridColumn { Field = "Fee", HeaderText = "Fee", Type = UiColumnType.Number, Width = 90, TextAlign = UiTextAlign.Right, Format = "C2", EditType = UiEditType.NumericEdit, ValidationRules = new UiValidationRules { Required = true, Min = 50, Max = 500 } },
new UiGridColumn { Field = "Notes", HeaderText = "Notes", Width = 260 }
}
})
<style>
#CellEdit .badge { padding: 4px 10px; border-radius: 12px; color: #fff; font-size: 12px; font-weight: 500; }
#CellEdit .booked { background-color: #28a745; }
#CellEdit .canceled { background-color: #dc3545; }
#CellEdit .completed { background-color: #007bff; }
#CellEdit .waiting { background-color: #fd7e14; }
#CellEdit .type { padding: 2px 8px; border-radius: 4px; font-size: 12px; background: #eceff1; }
#CellEdit .type.emergency { background: #fdecea; color: #b71c1c; }
#CellEdit .type.lab { background: #f3e5f5; color: #6a1b9a; }
#CellEdit .type.follow { background: #e0f2f1; color: #00695c; }
#CellEdit .type.routine { background: #eceff1; color: #455a64; }
#CellEdit .doctor-cell { display: flex; align-items: center; gap: 8px; }
#CellEdit .doctor-initials { display: inline-flex; align-items: center; justify-content: center; width: 26px; height: 26px; border-radius: 50%; background: #5c6bc0; color: #fff; font-size: 11px; font-weight: 600; }
</style>
@Html.UiGrid(new UiGridOptions
{
Id = "CellEdit",
Height = "400px",
OnActionBegin = "actionBegin",
OnQueryCellInfo = "queryCellInfo",
Data = ViewData["dataSource"],
RowHeight = 40,
AllowSorting = true,
AllowFiltering = true,
AllowPaging = true,
PageSize = 12,
FilterType = UiGridFilterType.CheckBox,
EditSettings = new UiEditSettings { AllowAdding = true, AllowEditing = true, AllowDeleting = true, Mode = UiEditMode.Cell },
Toolbar = new List<string> { "Add", "Delete", "Update", "Cancel" },
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "ApptID", HeaderText = "Appointment ID", IsPrimaryKey = true, Width = 140, Visible = false },
new UiGridColumn { Field = "Patient", HeaderText = "Patient", Width = 150, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "Doctor", HeaderText = "Doctor", Width = 160, EditType = UiEditType.DropDownEdit, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "AppointmentTime", HeaderText = "Appointment Time", Type = UiColumnType.Date, Width = 200, Format = "M/d/yyyy hh:mm tt", EditType = UiEditType.DateTimePickerEdit },
new UiGridColumn { Field = "Type", HeaderText = "Type", Width = 150, EditType = UiEditType.DropDownEdit, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "Status", HeaderText = "Status", Width = 130, EditType = UiEditType.DropDownEdit, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "Room", HeaderText = "Room No", Width = 120, EditType = UiEditType.DropDownEdit },
new UiGridColumn { Field = "Fee", HeaderText = "Fee", Type = UiColumnType.Number, Width = 90, TextAlign = UiTextAlign.Right, Format = "C2", EditType = UiEditType.NumericEdit, ValidationRules = new UiValidationRules { Required = true, Min = 50, Max = 500 } },
new UiGridColumn { Field = "Notes", HeaderText = "Notes", Width = 260 }
}
})
<script>
var doctorRoomMap = { 'Dr. Smitha': 'R1', 'Dr. Johnson': 'R2', 'Dr. Garcia': 'R6', 'Dr. Brianna': 'R4', 'Dr. Williams': 'R3', 'Dr. Martinez': 'R7', 'Dr. Davis': 'R8', 'Dr. Joanna': 'R5' };
// Syncfusion: a new appointment gets an id; choosing a doctor puts the doctor's room in the Room column.
function actionBegin(args) {
if (args.requestType === 'save' && args.action === 'add') {
args.data.ApptID = 'APT-' + (Date.now() % 100000);
}
if (args.requestType === 'save' && args.field === 'Doctor' && doctorRoomMap[args.data.Doctor]) {
args.data.Room = doctorRoomMap[args.data.Doctor];
}
}
// Syncfusion builds the Doctor, Type and Status cells with template functions; here one function fills them (the doctor's photo is the initials).
function queryCellInfo(args) {
if (args.column === 'Doctor') {
var cell = document.createElement('div');
cell.className = 'doctor-cell';
var initials = document.createElement('span');
initials.className = 'doctor-initials';
initials.textContent = String(args.data.Doctor || '').replace('Dr. ', '').charAt(0);
var name = document.createElement('span');
name.textContent = args.data.Doctor;
cell.appendChild(initials);
cell.appendChild(name);
args.content = cell;
} else if (args.column === 'Status') {
var badge = document.createElement('span');
badge.className = 'badge ' + ({ Booked: 'booked', Canceled: 'canceled', Completed: 'completed' }[args.data.Status] || 'waiting');
badge.textContent = args.data.Status;
args.content = badge;
} else if (args.column === 'Type') {
var type = document.createElement('span');
type.className = 'type ' + ({ 'Emergency': 'emergency', 'Lab Test': 'lab', 'Follow-up': 'follow', 'Routine Check': 'routine' }[args.data.Type] || '');
type.textContent = args.data.Type;
args.content = type;
}
}
</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/CellEditing.cshtml; it draws what ViewData holds.
public ActionResult CellEditing()
{
var order = AppointmentDetails.GetAllRecords();
ViewData["dataSource"] = order;
return View();
}
public class AppointmentDetails
{
public AppointmentDetails() { }
public AppointmentDetails(string apptID, DateTime appointmentTime, string patient, string doctor,
string room, string type, string status, double fee, string notes)
{
this.ApptID = apptID;
this.AppointmentTime = appointmentTime;
this.Patient = patient;
this.Doctor = doctor;
this.Room = room;
this.Type = type;
this.Status = status;
this.Fee = fee;
this.Notes = notes;
}
public static List<AppointmentDetails> GetAllRecords()
{
List<AppointmentDetails> data = new List<AppointmentDetails>()
{
new AppointmentDetails("APT-1001", new DateTime(2025,12,10,11,30,0), "John Miller", "Dr. Martinez", "R7", "Consultation", "Booked", 50, "Requires follow-up in 2 weeks"),
new AppointmentDetails("APT-1002", new DateTime(2025,12,10,12,0,0), "Emma Wilson", "Dr. Smitha", "R1", "Follow-up", "Canceled", 75, "Prescribed medication after evaluation"),
new AppointmentDetails("APT-1003", new DateTime(2025,12,10,14,0,0), "Liam Davis", "Dr. Garcia", "R6", "Emergency", "Completed", 70, "Vitals are stable and normal"),
new AppointmentDetails("APT-1004", new DateTime(2025,12,10,14,30,0), "Olivia Brown", "Dr. Brianna", "R4", "Routine Check", "Pending", 150, "Lab test recommended for further diagnosis"),
new AppointmentDetails("APT-1005", new DateTime(2025,12,11,11,30,0), "Noah Taylor", "Dr. Davis", "R8", "Lab Test", "Booked", 200, "Under observation for short duration"),
new AppointmentDetails("APT-1006", new DateTime(2025,12,11,12,0,0), "Ava Anderson", "Dr. Johnson", "R2", "Consultation", "Completed", 125, "Follow-up completed successfully"),
new AppointmentDetails("APT-1007", new DateTime(2025,12,11,14,0,0), "James Thomas", "Dr. Williams", "R3", "Emergency", "Booked", 300, "Patient condition improving steadily"),
new AppointmentDetails("APT-1008", new DateTime(2025,12,11,15,0,0), "Sophia Jackson", "Dr. Smitha", "R1", "Routine Check", "Pending", 120, "Initial diagnosis recorded"),
new AppointmentDetails("APT-1009", new DateTime(2025,12,12,11,30,0), "Lucas White", "Dr. Brianna", "R4", "Follow-up", "Completed", 180, "Further evaluation needed"),
new AppointmentDetails("APT-1010", new DateTime(2025,12,12,12,0,0), "Mia Harris", "Dr. Davis", "R8", "Consultation", "Booked", 220, "Routine check completed"),
new AppointmentDetails("APT-1011", new DateTime(2025,12,12,14,0,0), "Ethan Martin", "Dr. Garcia", "R6", "Lab Test", "Pending", 90, "Medication adjusted"),
new AppointmentDetails("APT-1012", new DateTime(2025,12,12,15,30,0), "Amelia Thompson", "Dr. Martinez", "R7", "Emergency", "Canceled", 160, "Diet plan suggested"),
new AppointmentDetails("APT-1013", new DateTime(2025,12,13,11,30,0), "Daniel Garcia", "Dr. Johnson", "R2", "Consultation", "Booked", 210, "Symptoms reduced"),
new AppointmentDetails("APT-1014", new DateTime(2025,12,13,12,0,0), "Harper Martinez", "Dr. Williams", "R3", "Follow-up", "Completed", 260, "Referred to specialist"),
new AppointmentDetails("APT-1015", new DateTime(2025,12,13,14,30,0), "Henry Robinson", "Dr. Smitha", "R1", "Routine Check", "Pending", 80, "Scheduled for scan"),
new AppointmentDetails("APT-1016", new DateTime(2025,12,13,16,0,0), "Ella Clark", "Dr. Joanna", "R5", "Lab Test", "Booked", 140, "Test results reviewed"),
new AppointmentDetails("APT-1017", new DateTime(2025,12,10,15,0,0), "Alexander Rodriguez", "Dr. Garcia", "R6", "Emergency", "Completed", 170, "Condition stable"),
new AppointmentDetails("APT-1018", new DateTime(2025,12,10,16,30,0), "Grace Lewis", "Dr. Martinez", "R7", "Consultation", "Booked", 230, "Recovery in progress"),
new AppointmentDetails("APT-1019", new DateTime(2025,12,10,17,30,0), "Michael Lee", "Dr. Davis", "R8", "Follow-up", "Pending", 270, "Minor symptoms present"),
new AppointmentDetails("APT-1020", new DateTime(2025,12,11,15,30,0), "Chloe Walker", "Dr. Smitha", "R1", "Routine Check", "Canceled", 60, "Follow-up next month"),
new AppointmentDetails("APT-1021", new DateTime(2025,12,11,16,30,0), "Benjamin Hall", "Dr. Brianna", "R4", "Lab Test", "Booked", 110, "Observation continued"),
new AppointmentDetails("APT-1022", new DateTime(2025,12,11,17,30,0), "Zoe Allen", "Dr. Johnson", "R2", "Consultation", "Completed", 190, "Treatment ongoing"),
new AppointmentDetails("APT-1023", new DateTime(2025,12,12,16,30,0), "Jacob Young", "Dr. Williams", "R3", "Emergency", "Pending", 240, "Improvement noted"),
new AppointmentDetails("APT-1024", new DateTime(2025,12,12,17,30,0), "Lily King", "Dr. Garcia", "R6", "Routine Check", "Booked", 280, "Discharge planned"),
new AppointmentDetails("APT-1025", new DateTime(2025,12,13,15,0,0), "Logan Wright", "Dr. Martinez", "R7", "Follow-up", "Completed", 130, "Checkup completed"),
new AppointmentDetails("APT-1026", new DateTime(2025,12,13,16,30,0), "Aria Scott", "Dr. Smitha", "R1", "Consultation", "Booked", 155, "Additional tests advised"),
new AppointmentDetails("APT-1027", new DateTime(2025,12,13,17,30,0), "David Green", "Dr. Brianna", "R4", "Lab Test", "Pending", 205, "Rest recommended"),
new AppointmentDetails("APT-1028", new DateTime(2025,12,12,15,0,0), "Scarlett Adams", "Dr. Davis", "R8", "Emergency", "Canceled", 225, "Fluid intake increased"),
new AppointmentDetails("APT-1029", new DateTime(2025,12,10,16,0,0), "Joseph Baker", "Dr. Johnson", "R2", "Routine Check", "Booked", 265, "Short-term medication given"),
new AppointmentDetails("APT-1030", new DateTime(2025,12,10,17,0,0), "Layla Nelson", "Dr. Williams", "R3", "Follow-up", "Completed", 295, "Monitoring advised")
};
return data;
}
public string ApptID { get; set; }
public DateTime AppointmentTime { get; set; }
public string Patient { get; set; }
public string Doctor { get; set; }
public string Room { get; set; }
public string Type { get; set; }
public string Status { get; set; }
public double Fee { get; set; }
public string Notes { get; set; }
}
}
}
@* 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. *@
Cell editing allows users to modify a single cell’s value directly. This mode is enabled by setting editSettings.mode to Cell. Users can enter edit mode by double‑clicking a cell and then changing its value. The update is applied when the user presses "Enter" key or moves to another cell.
This editing mode works seamlessly with other Grid features such as validation, formatting, and more, ensuring a consistent and efficient editing experience.