Example of Overview in ASP.NET MVC Data Grid Control
The basic grid features on large data, with the load time: pick how many rows and columns in the dropdown. Virtual scrolling, filtering, sorting, checkbox selection and column templates.
@Html.AntiForgeryToken()
@Html.UiGrid(new UiGridOptions
{
Id = "overviewGrid",
DataUrl = Url.Action("Employees", "GridDemo"),
DataUrlParams = new { rows = 1000 },
ServerOperations = false, // all rows once; sorting and filtering in the browser
EnableVirtualization = true, // only the rows in view are drawn
Height = "600px",
AllowFiltering = true,
AllowSorting = true,
AllowMultiSorting = true,
Selection = UiGridSelection.Multiple,
ContextMenuItems = new List<object> { "Copy", "SortAscending", "SortDescending", "AutoFit", "AutoFitAll", "ExcelExport", "PdfExport" },
OnDataLoaded = "Overview.loaded",
Columns = new List<UiGridColumn>
{
new UiGridColumn { Type = UiColumnType.CheckBox, Freeze = UiFreezeDirection.Left },
new UiGridColumn { Field = "EmployeeId", HeaderText = "Employee ID", Type = UiColumnType.Number, IsPrimaryKey = true, Width = 130 },
new UiGridColumn { Field = "Name", HeaderText = "Employee Name", Template = "#ovEmployee", Width = 210 },
new UiGridColumn { Field = "Designation", Width = 160 },
new UiGridColumn { Field = "Mail", Width = 220 },
new UiGridColumn { Field = "Location", Template = "#ovLocation", Width = 140 },
new UiGridColumn { Field = "Status", Template = "#ovStatus", Width = 130 },
new UiGridColumn { Field = "Trustworthiness", Template = "#ovTrust", Width = 170 },
new UiGridColumn { Field = "Rating", Type = UiColumnType.Number, Template = "#ovRating", TextAlign = UiTextAlign.Left, Width = 150 },
new UiGridColumn { Field = "Software", HeaderText = "Software Proficiency", Type = UiColumnType.Number, Template = "#ovSoftware", TextAlign = UiTextAlign.Left, Width = 190 },
new UiGridColumn { Field = "CurrentSalary", HeaderText = "Current Salary", Type = UiColumnType.Number, Format = "C0", Width = 150 },
new UiGridColumn { Field = "Address", Width = 200 }
}
})
@Html.AntiForgeryToken()
<style>
.ov-bar { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.ov-size { min-width: 220px; max-width: none; height: 34px; padding: 0 10px; border: 1px solid var(--ui-border, #d1d1d1); border-radius: 6px; background: var(--ui-surface, #fff); color: inherit; font: inherit; }
.ov-time { color: var(--ui-muted, #616161); font-size: 13px; }
.ov-time b { color: var(--ui-text, #242424); }
.ov-emp { display: inline-flex; align-items: center; gap: 10px; }
.ov-avatar { display: inline-flex; align-items: center; justify-content: center; flex: none; width: 28px; height: 28px; border-radius: 50%; background: #d1335b; color: #fff; font-size: 11px; font-weight: 700; }
.ov-loc { display: inline-flex; align-items: center; gap: 6px; }
.ov-loc svg { width: 14px; height: 14px; color: #616161; }
.ov-status { display: inline-block; padding: 0 8px; border-radius: 4px; font-size: 13px; line-height: 20px; }
.ov-status.is-Active { background: #dff6dd; color: #107c10; }
.ov-status.is-Inactive { background: #fde7e9; color: #c50f1f; }
.ov-trust { display: inline-flex; align-items: center; gap: 6px; }
.ov-trust svg { width: 14px; height: 14px; }
.ov-trust.is-Perfect svg { color: #107c10; }
.ov-trust.is-Sufficient svg { color: #0f6cbd; }
.ov-trust.is-Insufficient svg { color: #c50f1f; }
.ov-stars { position: relative; display: inline-block; width: 90px; height: 16px; background: #d6d6d6;
-webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 90 16'%3E%3Cpath id='s' d='M8 1l2.1 4.6 5 .5-3.8 3.3 1.1 4.9L8 11.8 3.6 14.3l1.1-4.9L.9 6.1l5-.5z'/%3E%3Cuse href='%23s' x='18'/%3E%3Cuse href='%23s' x='36'/%3E%3Cuse href='%23s' x='54'/%3E%3Cuse href='%23s' x='72'/%3E%3C/svg%3E") 0 0 / 90px 16px no-repeat;
mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 90 16'%3E%3Cpath id='s' d='M8 1l2.1 4.6 5 .5-3.8 3.3 1.1 4.9L8 11.8 3.6 14.3l1.1-4.9L.9 6.1l5-.5z'/%3E%3Cuse href='%23s' x='18'/%3E%3Cuse href='%23s' x='36'/%3E%3Cuse href='%23s' x='54'/%3E%3Cuse href='%23s' x='72'/%3E%3C/svg%3E") 0 0 / 90px 16px no-repeat; }
.ov-stars::before { content: ""; position: absolute; inset: 0 auto 0 0; background: #ffa800; }
.ov-stars.is-1::before { width: 16px; } .ov-stars.is-2::before { width: 34px; } .ov-stars.is-3::before { width: 52px; }
.ov-stars.is-4::before { width: 70px; } .ov-stars.is-5::before { width: 90px; }
.ov-software { display: block; width: 100%; height: 16px; }
.ov-software span { display: block; height: 100%; padding-left: 4px; color: #fff; font-size: 10px; font-weight: 700; line-height: 16px; }
.ov-software.is-high span { background: #1cb21c; }
.ov-software.is-low span { background: #e31e1e; }
</style>
<template id="ovEmployee"><span class="ov-emp"><span class="ov-avatar">${Initials}</span><span>${Name}</span></span></template>
<template id="ovLocation"><span class="ov-loc"><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.3"><path d="M8 14.5s4.5-4.2 4.5-7.7a4.5 4.5 0 1 0-9 0C3.5 10.3 8 14.5 8 14.5z"/><circle cx="8" cy="6.8" r="1.6"/></svg>${Location}</span></template>
<template id="ovStatus"><span class="ov-status is-${Status}">${Status}</span></template>
<template id="ovTrust"><span class="ov-trust is-${Trustworthiness}"><svg viewBox="0 0 16 16" fill="currentColor"><path d="M3 1.5h1.3v13H3z"/><path d="M4.3 2.2c2.6-1.2 4.4 1 7.9-.2v6.3c-3.5 1.2-5.3-1-7.9.2z"/></svg>${Trustworthiness}</span></template>
<template id="ovRating"><span class="ov-stars is-${Rating}" role="img" aria-label="${Rating} of 5"></span></template>
<template id="ovSoftware"><span class="ov-software is-${SoftwareLevel}"><span style="width: ${Software}%">${Software}%</span></span></template>
<div class="ov-bar">
<select class="ov-size" id="ovSize" aria-label="Rows and columns">
<option value="1000|11" selected>1,000 Rows 11 Columns</option>
<option value="10000|11">10,000 Rows 11 Columns</option>
<option value="100000|11">100,000 Rows 11 Columns</option>
<option value="1000|6">1,000 Rows 6 Columns</option>
<option value="100000|6">100,000 Rows 6 Columns</option>
</select>
<span class="ov-time">Load Time: <b id="ovTime">-</b></span>
</div>
@Html.UiGrid(new UiGridOptions
{
Id = "overviewGrid",
DataUrl = Url.Action("Employees", "GridDemo"),
DataUrlParams = new { rows = 1000 },
ServerOperations = false, // all rows once; sorting and filtering in the browser
EnableVirtualization = true, // only the rows in view are drawn
Height = "600px",
AllowFiltering = true,
AllowSorting = true,
AllowMultiSorting = true,
Selection = UiGridSelection.Multiple,
ContextMenuItems = new List<object> { "Copy", "SortAscending", "SortDescending", "AutoFit", "AutoFitAll", "ExcelExport", "PdfExport" },
OnDataLoaded = "Overview.loaded",
Columns = new List<UiGridColumn>
{
new UiGridColumn { Type = UiColumnType.CheckBox, Freeze = UiFreezeDirection.Left },
new UiGridColumn { Field = "EmployeeId", HeaderText = "Employee ID", Type = UiColumnType.Number, IsPrimaryKey = true, Width = 130 },
new UiGridColumn { Field = "Name", HeaderText = "Employee Name", Template = "#ovEmployee", Width = 210 },
new UiGridColumn { Field = "Designation", Width = 160 },
new UiGridColumn { Field = "Mail", Width = 220 },
new UiGridColumn { Field = "Location", Template = "#ovLocation", Width = 140 },
new UiGridColumn { Field = "Status", Template = "#ovStatus", Width = 130 },
new UiGridColumn { Field = "Trustworthiness", Template = "#ovTrust", Width = 170 },
new UiGridColumn { Field = "Rating", Type = UiColumnType.Number, Template = "#ovRating", TextAlign = UiTextAlign.Left, Width = 150 },
new UiGridColumn { Field = "Software", HeaderText = "Software Proficiency", Type = UiColumnType.Number, Template = "#ovSoftware", TextAlign = UiTextAlign.Left, Width = 190 },
new UiGridColumn { Field = "CurrentSalary", HeaderText = "Current Salary", Type = UiColumnType.Number, Format = "C0", Width = 150 },
new UiGridColumn { Field = "Address", Width = 200 }
}
})
<div class="demo-sample-about">
<p>
This sample shows the grid's everyday features on a large list of employees. Choose up to 100,000 rows
in the dropdown: the grid reads them once from the <code>Employees</code> action
(<code>ServerOperations = false</code>) and, with <code>EnableVirtualization</code>, draws only the rows in
view while you scroll. <b>Load Time</b> is the time from the request to the rows on screen.
</p>
<p>
The columns use templates (<code>Template = "#id"</code>, a <code><template></code> with <code>${Field}</code>
placeholders) for the avatar, location, status, trust, rating stars and the proficiency bar. Sort by clicking
a header (Ctrl+click for more columns), filter from the filter bar, select rows with the checkboxes, and
right-click for the context menu.
</p>
<p>
The number of rows goes to the server with the request: <code>DataUrlParams</code> for the first one,
<code>grid.reload({ rows })</code> when the dropdown changes. Fewer columns are
<code>grid.hideColumns(...)</code>.
</p>
</div>
@section scripts {
<script>
window.Overview = (function () {
'use strict';
var started = performance.now();
var measuring = true;
var FEWER = ['EmployeeId', 'Mail', 'Trustworthiness', 'CurrentSalary', 'Address'];
function init() {
var size = document.getElementById('ovSize');
if (!size) { return; }
size.addEventListener('change', function () {
var parts = this.value.split('|');
var grid = window.Lumen && window.Lumen.get('overviewGrid');
if (!grid) { return; }
if (parts[1] === '6') { grid.hideColumns(FEWER); } else { grid.showColumns(FEWER); }
started = performance.now();
measuring = true;
var time = document.getElementById('ovTime');
if (time) { time.textContent = '...'; }
grid.reload({ rows: Number(parts[0]) });
});
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
return {
loaded: function () {
var time = document.getElementById('ovTime');
if (!measuring || !time) { return; }
measuring = false;
time.textContent = Math.round(performance.now() - started) + 'ms';
}
};
})();
</script>
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
using Lumen.UI.Mvc.Demo.Models;
using Newtonsoft.Json;
// ServerOperations = true (default): the grid POSTs a UiDataRequest and expects { result, count } back (UiDataOperations.Execute).
// ServerOperations = false: return all rows once as a plain JSON array.
namespace Lumen.UI.Mvc.Demo.Controllers
{
public partial class GridDemoController : Controller
{
/// <summary>
/// The overview's employees: the first `rows` of 100,000 (1,000 / 10,000 / 100,000, chosen in the page and sent
/// as a DataUrlParams value), all at once as a plain array (ServerOperations = false): the grid keeps them in
/// the browser and draws only the rows in view. Json.NET writes them (JsonResult stops at 4 MB).
/// </summary>
[HttpPost]
[ValidateJsonAntiForgeryToken]
public ActionResult Employees(int rows = 1000)
{
rows = Math.Max(1, Math.Min(100000, rows));
return Content(JsonConvert.SerializeObject(GridFeatureData.Employees().Take(rows)), "application/json");
}
}
}
namespace Lumen.UI.Mvc.Demo.Models
{
/// <summary>An employee of the grid overview (large data: up to 100,000 rows).</summary>
public class Employee
{
public int EmployeeId { get; set; }
public string Name { get; set; }
public string Initials { get; set; }
public string Designation { get; set; }
public string Mail { get; set; }
public string Location { get; set; }
public string Status { get; set; }
public string Trustworthiness { get; set; }
public int Rating { get; set; }
public int Software { get; set; }
/// <summary>"high" (40% and more) or "low": the colour of the Software bar.</summary>
public string SoftwareLevel
{
get { return Software >= 40 ? "high" : "low"; }
}
public decimal CurrentSalary { get; set; }
public string Address { get; set; }
}
}
namespace Lumen.UI.Mvc.Demo.Models
{
/// <summary>In-memory data of the grid feature and showcase pages. A real app would read a database.</summary>
public static class GridFeatureData
{
private static List<Employee> employees;
/// <summary>100,000 made-up employees (built once); the overview takes the first 1,000, 10,000 or all of them.</summary>
public static List<Employee> Employees()
{
// The demo builds made-up rows here (the fields are in the class above); a real app queries its database.
throw new NotImplementedException("Return your own 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. *@
This sample shows the grid's everyday features on a large list of employees. Choose up to 100,000 rows
in the dropdown: the grid reads them once from the Employees action
(ServerOperations = false) and, with EnableVirtualization, draws only the rows in
view while you scroll. Load Time is the time from the request to the rows on screen.
The columns use templates (Template = "#id", a <template> with ${Field}
placeholders) for the avatar, location, status, trust, rating stars and the proficiency bar. Sort by clicking
a header (Ctrl+click for more columns), filter from the filter bar, select rows with the checkboxes, and
right-click for the context menu.
The number of rows goes to the server with the request: DataUrlParams for the first one,
grid.reload({ rows }) when the dropdown changes. Fewer columns are
grid.hideColumns(...).