Example of Column Virtualization in ASP.NET Core Data Grid Control
40 columns with only those in view drawn, and a header that sticks to the top of the window.
40 columns with EnableColumnVirtualization: only the columns in view are drawn while scrolling
sideways. EnableStickyHeader: scroll the page down and the header stays at the top of the window.
@Html.UiGrid(new UiGridOptions
{
Id = "wideGrid",
Data = wideRows,
Columns = wideColumns,
Layout = UiGridLayout.FitData,
EnableColumnVirtualization = true,
EnableStickyHeader = true,
PageSize = 30
})
@using Lumen.UI.Mvc.Demo.Models
@model System.Collections.Generic.List<Lumen.UI.Mvc.Demo.Models.Order>
@{
// A wide table for column virtualization: 40 columns, 60 rows.
var wideRows = new List<Dictionary<string, object>>();
for (int r = 1; r <= 60; r++)
{
var row = new Dictionary<string, object> { { "Row", r } };
for (int c = 1; c <= 40; c++) { row["C" + c] = r * 100 + c; }
wideRows.Add(row);
}
var wideColumns = new List<UiGridColumn> { new UiGridColumn { Field = "Row", Type = UiColumnType.Number, Width = 70, Freeze = UiFreezeDirection.Left } };
for (int c = 1; c <= 40; c++) { wideColumns.Add(new UiGridColumn { Field = "C" + c, HeaderText = "Column " + c, Type = UiColumnType.Number, Width = 120 }); }
}
<p>
40 columns with <code>EnableColumnVirtualization</code>: only the columns in view are drawn while scrolling
sideways. <code>EnableStickyHeader</code>: scroll the page down and the header stays at the top of the window.
</p>
@Html.UiGrid(new UiGridOptions
{
Id = "wideGrid",
Data = wideRows,
Columns = wideColumns,
Layout = UiGridLayout.FitData,
EnableColumnVirtualization = true,
EnableStickyHeader = true,
PageSize = 30
})
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.
// Inside the Sample action of GridDemoController:
return View("More", CopyOrders(SampleOrders.All.Take(20)));
namespace Lumen.UI.Mvc.Demo.Controllers
{
public partial class GridDemoController : Controller
{
// SampleOrders.All is a shared in-memory list (also read by the chart demo); a real app would use its database instead.
private static readonly object Sync = SampleOrders.Sync;
private static List<Order> CopyOrders(IEnumerable<Order> orders)
{
// Copies, so the in-page edits never change the shared list.
return orders.Select(o => new Order
{
Id = o.Id, OrderNo = o.OrderNo, OrderDate = o.OrderDate, Customer = o.Customer, CustomerId = o.CustomerId,
Region = o.Region, Amount = o.Amount, Shipped = o.Shipped, Notes = o.Notes
}).ToList();
}
}
}
namespace Lumen.UI.Mvc.Demo.Models
{
public class Order
{
public int Id { get; set; }
public string OrderNo { get; set; }
public DateTime OrderDate { get; set; }
public string Customer { get; set; }
/// <summary>Key of the customer (see SampleOrders.Customers), to show a foreign key column.</summary>
public int CustomerId { get; set; }
/// <summary>North, South, East or West: a column with few values, to show grouping and a dropdown editor.</summary>
public string Region { get; set; }
public decimal Amount { get; set; }
public bool Shipped { get; set; }
/// <summary>Long free text, to show text wrapping in the grid.</summary>
public string Notes { get; set; }
}
}
namespace Lumen.UI.Mvc.Demo.Models
{
/// <summary>In-memory test data for the grid and chart demos. A real app would query a database instead.</summary>
public static class SampleOrders
{
/// <summary>Lock held while reading or changing <see cref="All"/> (the grid demo edits it).</summary>
public static readonly object Sync = new object();
private static readonly string[] Customers =
{
"Santos Trading", "Reyes Hardware", "Dela Cruz Foods", "Bautista Auto Parts",
"Garcia Textiles", "Mendoza Electronics", "Torres Logistics", "Flores Bakery"
};
// Long notes to show text wrapping; some contain one very long word (a part number without spaces),
// which is broken inside the word only because it is wider than the column.
private static readonly string[] Notes =
{
"Deliver to the back entrance before 10 AM; the front gate is closed on weekends and public holidays.",
"Customer asked for partial delivery: send the available items now and the rest when back in stock.",
"Replacement part PN-XJ9000-ULTRAHEAVYDUTY-STAINLESSSTEELFLANGEASSEMBLY-REV7 must be checked before shipping.",
"Fragile.",
"Invoice must show the purchase order number, the project code and the name of the receiving warehouse.",
"Supercalifragilisticexpialidociousnessmaximizationprogramme discount applied at checkout.",
"Call the site supervisor thirty minutes before arrival so the loading bay can be cleared.",
"Standard delivery."
};
/// <summary>The values of Order.Region: the grouping demo's groups and the dropdown editor's choices.</summary>
public static readonly string[] Regions = { "North", "South", "East", "West" };
public static readonly List<Order> All = Build();
private static List<Order> Build()
{
// 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.");
}
}
}
@* Program.cs *@
builder.Services.AddControllersWithViews().AddLumenUIJson();
builder.Services.AddLumenUI();
// ...
app.UseLumenUI();
@* Views/_ViewImports.cshtml *@
@using Lumen.UI.Mvc
@addTagHelper *, Lumen.UI.Core
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@await RenderSectionAsync("scripts", required: false)
</body>
@* Grids with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@