Example of Stacked Header in ASP.NET Core Data Grid Control
This example showcases the Syncfusion EJ2 Grid component which features stacked headers, column resizing, and a customizable column chooser template.
 
${ShipCountry}
@Html.UiGrid(new UiGridOptions
{
Id = "Grid",
Data = ViewData["StackedHeaderData"],
AllowSorting = true,
AllowMultiSorting = true,
AllowFiltering = true,
EnableHover = false,
OnQueryCellInfo = "queryCellInfo",
AllowResizing = true,
AllowPaging = true,
PageSize = 12,
FilterType = UiGridFilterType.Excel,
EditSettings = new UiEditSettings { AllowAdding = true, AllowEditing = true, AllowDeleting = true },
Toolbar = new List<string> { "Add", "Edit", "Delete", "Update", "Cancel", "ColumnChooser" },
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "CustomerID", HeaderText = "Customer ID", Type = UiColumnType.Number, Width = 160, MinWidth = 115, TextAlign = UiTextAlign.Right, IsPrimaryKey = true, ShowInColumnChooser = false, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "CustomerName", HeaderText = "Name", Width = 150, MinWidth = 120 },
new UiGridColumn { HeaderText = "Order Details", TextAlign = UiTextAlign.Center, Columns = new List<UiGridColumn> { new UiGridColumn { Field = "OrderID", Width = 90, Type = UiColumnType.Number, HeaderText = "ID", MinWidth = 90, TextAlign = UiTextAlign.Right }, new UiGridColumn { Field = "OrderDate", Width = 110, Type = UiColumnType.Date, TextAlign = UiTextAlign.Right, HeaderText = "Date", MinWidth = 105, Format = "M/d/yyyy", EditType = UiEditType.DatePickerEdit } } },
new UiGridColumn { HeaderText = "Shipping Details", TextAlign = UiTextAlign.Center, Columns = new List<UiGridColumn> { new UiGridColumn { Field = "ShipCountry", Width = 150, HeaderText = "Country", MinWidth = 120, Template = "#locationTemplate", EditType = UiEditType.DropDownEdit, ValidationRules = new UiValidationRules { Required = true } }, new UiGridColumn { Field = "Freight", Width = 130, Type = UiColumnType.Number, HeaderText = "Charges", TextAlign = UiTextAlign.Right, MinWidth = 115, Format = "C2", EditType = UiEditType.NumericEdit, ValidationRules = new UiValidationRules { Required = true } } } },
new UiGridColumn { HeaderText = "Delivery Details", TextAlign = UiTextAlign.Center, Columns = new List<UiGridColumn> { new UiGridColumn { Field = "Status", Width = 150, HeaderText = "Status", MinWidth = 120, TextAlign = UiTextAlign.Center, EditType = UiEditType.DropDownEdit, ValidationRules = new UiValidationRules { Required = true } }, new UiGridColumn { Field = "Feedback", Width = 130, Type = UiColumnType.Number, HeaderText = "Feedback", AllowResizing = false, MinWidth = 115, TextAlign = UiTextAlign.Center, EditType = UiEditType.NumericEdit, ValidationRules = new UiValidationRules { Required = true, Min = 0, Max = 5 } } } }
}
})
<style>
.e-rowcell-delivered { color: #137333; font-weight: 600; }
.e-rowcell-inprogress { color: #b06000; font-weight: 600; }
.feedback-stars { color: #e8a317; letter-spacing: 2px; white-space: nowrap; }
.feedback-stars .off { color: #cfcfcf; }
</style>
<template id="locationTemplate"><div class="image"><span>${ShipCountry}</span></div></template>
<style>
#column-chooser-icon, #column-chooser-text {
position: relative;
}
#column-chooser-icon {
top: 3px;
}
#column-chooser-text {
left: 5px;
}
.e-rtl #column-chooser-icon {
top: 3px;
right: 5px;
}
.e-rtl #column-chooser-text {
left: 0;
}
.no-record-text {
padding: 5px;
}
.custom-rating.e-rating-container .e-rating-item-container,
.e-bigger
.custom-rating.e-rating-container
.e-rating-item-container
.e-rating-icon,
.e-bigger.custom-rating.e-rating-container
.e-rating-item-container
.e-rating-icon {
font-size: 11px;
}
.custom-rating.e-rating-container
.e-rating-item-container
.e-rating-icon {
-webkit-text-fill-color: lightgrey;
-webkit-text-stroke: 1px lightgrey;
}
.custom-rating.e-rating-container
.e-rating-item-container.e-rating-selected
.e-rating-icon {
background: linear-gradient(
to right,
#ffa600 var(--rating-value),
#ffa600 var(--rating-value)
);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
-webkit-text-stroke: 1px #ffa600;
}
.e-image {
height: 13px;
width: 14px;
}
.fluent .e-grid .e-rowcell.e-delivered,
.fluent2 .e-grid .e-rowcell.e-delivered,
.bootstrap5 .e-grid .e-rowcell.e-delivered,
.bootstrap5\.3 .e-grid .e-rowcell.e-delivered,
.tailwind3 .e-grid .e-rowcell.e-delivered,
.tailwind .e-grid .e-rowcell.e-delivered,
.material .e-grid .e-rowcell.e-delivered,
.fabric .e-grid .e-rowcell.e-delivered,
.bootstrap4 .e-grid .e-rowcell.e-delivered,
.bootstrap .e-grid .e-rowcell.e-delivered,
.highcontrast .e-grid .e-rowcell.e-delivered,
.fluent
.e-grid
.e-rowcell.e-delivered:not(.e-editedbatchcell):not(.e-updatedtd),
.fluent2
.e-grid
.e-rowcell.e-delivered:not(.e-editedbatchcell):not(.e-updatedtd),
.bootstrap5
.e-grid
.e-rowcell.e-delivered:not(.e-editedbatchcell):not(.e-updatedtd),
.bootstrap5\.3
.e-grid
.e-rowcell.e-delivered:not(.e-editedbatchcell):not(.e-updatedtd),
.tailwind3
.e-grid
.e-rowcell.e-delivered:not(.e-editedbatchcell):not(.e-updatedtd),
.tailwind
.e-grid
.e-rowcell.e-delivered:not(.e-editedbatchcell):not(.e-updatedtd),
.material
.e-grid
.e-rowcell.e-delivered:not(.e-editedbatchcell):not(.e-updatedtd),
.fabric
.e-grid
.e-rowcell.e-delivered:not(.e-editedbatchcell):not(.e-updatedtd),
.bootstrap4
.e-grid
.e-rowcell.e-delivered:not(.e-editedbatchcell):not(.e-updatedtd),
.bootstrap
.e-grid
.e-rowcell.e-delivered:not(.e-editedbatchcell):not(.e-updatedtd),
.highcontrast
.e-grid
.e-rowcell.e-delivered:not(.e-editedbatchcell):not(.e-updatedtd),
.e-grid .e-rowcell.e-delivered,
.e-grid
.e-rowcell.e-delivered:not(.e-editedbatchcell):not(.e-updatedtd) {
background-color: #dfffe2;
color: #00a653;
}
.fluent-dark .e-grid .e-rowcell.e-delivered,
.fluent2-dark .e-grid .e-rowcell.e-delivered,
.fluent2-highcontrast .e-grid .e-rowcell.e-delivered,
.bootstrap5-dark .e-grid .e-rowcell.e-delivered,
.bootstrap5\.3-dark .e-grid .e-rowcell.e-delivered,
.highcontrast .e-grid .e-rowcell.e-delivered,
.tailwind3-dark .e-grid .e-rowcell.e-delivered,
.tailwind-dark .e-grid .e-rowcell.e-delivered,
.material-dark .e-grid .e-rowcell.e-delivered,
.fabric-dark .e-grid .e-rowcell.e-delivered,
.bootstrap-dark .e-grid .e-rowcell.e-delivered,
.fluent-dark
.e-grid
.e-rowcell.e-delivered:not(.e-editedbatchcell):not(.e-updatedtd),
.fluent2-dark
.e-grid
.e-rowcell.e-delivered:not(.e-editedbatchcell):not(.e-updatedtd),
.fluent2-highcontrast
.e-grid
.e-rowcell.e-delivered:not(.e-editedbatchcell):not(.e-updatedtd),
.bootstrap5-dark
.e-grid
.e-rowcell.e-delivered:not(.e-editedbatchcell):not(.e-updatedtd),
.bootstrap5\.3-dark
.e-grid
.e-rowcell.e-delivered:not(.e-editedbatchcell):not(.e-updatedtd),
.highcontrast
.e-grid
.e-rowcell.e-delivered:not(.e-editedbatchcell):not(.e-updatedtd),
.tailwind3-dark
.e-grid
.e-rowcell.e-delivered:not(.e-editedbatchcell):not(.e-updatedtd),
.tailwind-dark
.e-grid
.e-rowcell.e-delivered:not(.e-editedbatchcell):not(.e-updatedtd),
.material-dark
.e-grid
.e-rowcell.e-delivered:not(.e-editedbatchcell):not(.e-updatedtd),
.fabric-dark
.e-grid
.e-rowcell.e-delivered:not(.e-editedbatchcell):not(.e-updatedtd),
.bootstrap-dark
.e-grid
.e-rowcell.e-delivered:not(.e-editedbatchcell):not(.e-updatedtd) {
background-color: #315c35;
color: #38ff9c;
}
.material3-dark .e-grid .e-rowcell.e-delivered,
.material3-dark
.e-grid
.e-rowcell.e-delivered:not(.e-editedbatchcell):not(.e-updatedtd) {
background-color: #143e17;
color: #8eda94;
}
.fluent .e-grid .e-rowcell.e-inprogress,
.fluent2 .e-grid .e-rowcell.e-inprogress,
.bootstrap5 .e-grid .e-rowcell.e-inprogress,
.bootstrap5\.3 .e-grid .e-rowcell.e-inprogress,
.tailwind3 .e-grid .e-rowcell.e-inprogress,
.tailwind .e-grid .e-rowcell.e-inprogress,
.material .e-grid .e-rowcell.e-inprogress,
.fabric .e-grid .e-rowcell.e-inprogress,
.bootstrap4 .e-grid .e-rowcell.e-inprogress,
.bootstrap .e-grid .e-rowcell.e-inprogress,
.highcontrast .e-grid .e-rowcell.e-inprogress,
.fluent
.e-grid
.e-rowcell.e-inprogress:not(.e-editedbatchcell):not(.e-updatedtd),
.fluent2
.e-grid
.e-rowcell.e-inprogress:not(.e-editedbatchcell):not(.e-updatedtd),
.bootstrap5
.e-grid
.e-rowcell.e-inprogress:not(.e-editedbatchcell):not(.e-updatedtd),
.bootstrap5\.3
.e-grid
.e-rowcell.e-inprogress:not(.e-editedbatchcell):not(.e-updatedtd),
.tailwind3
.e-grid
.e-rowcell.e-inprogress:not(.e-editedbatchcell):not(.e-updatedtd),
.tailwind
.e-grid
.e-rowcell.e-inprogress:not(.e-editedbatchcell):not(.e-updatedtd),
.material
.e-grid
.e-rowcell.e-inprogress:not(.e-editedbatchcell):not(.e-updatedtd),
.fabric
.e-grid
.e-rowcell.e-inprogress:not(.e-editedbatchcell):not(.e-updatedtd),
.bootstrap4
.e-grid
.e-rowcell.e-inprogress:not(.e-editedbatchcell):not(.e-updatedtd),
.bootstrap
.e-grid
.e-rowcell.e-inprogress:not(.e-editedbatchcell):not(.e-updatedtd),
.highcontrast
.e-grid
.e-rowcell.e-inprogress:not(.e-editedbatchcell):not(.e-updatedtd),
.e-grid .e-rowcell.e-inprogress,
.e-grid
.e-rowcell.e-inprogress:not(.e-editedbatchcell):not(.e-updatedtd) {
background-color: #ffebe9;
color: #ff3740;
}
.fluent-dark .e-grid .e-rowcell.e-inprogress,
.fluent2-dark .e-grid .e-rowcell.e-inprogress,
.fluent2-highcontrast .e-grid .e-rowcell.e-inprogress,
.bootstrap5-dark .e-grid .e-rowcell.e-inprogress,
.bootstrap5\.3-dark .e-grid .e-rowcell.e-inprogress,
.highcontrast .e-grid .e-rowcell.e-inprogress,
.tailwind3-dark .e-grid .e-rowcell.e-inprogress,
.tailwind-dark .e-grid .e-rowcell.e-inprogress,
.material-dark .e-grid .e-rowcell.e-inprogress,
.fabric-dark .e-grid .e-rowcell.e-inprogress,
.bootstrap-dark .e-grid .e-rowcell.e-inprogress,
.fluent-dark
.e-grid
.e-rowcell.e-inprogress:not(.e-editedbatchcell):not(.e-updatedtd),
.fluent2-dark
.e-grid
.e-rowcell.e-inprogress:not(.e-editedbatchcell):not(.e-updatedtd),
.fluent2-highcontrast
.e-grid
.e-rowcell.e-inprogress:not(.e-editedbatchcell):not(.e-updatedtd),
.bootstrap5-dark
.e-grid
.e-rowcell.e-inprogress:not(.e-editedbatchcell):not(.e-updatedtd),
.bootstrap5\.3-dark
.e-grid
.e-rowcell.e-inprogress:not(.e-editedbatchcell):not(.e-updatedtd),
.highcontrast
.e-grid
.e-rowcell.e-inprogress:not(.e-editedbatchcell):not(.e-updatedtd),
.tailwind3-dark
.e-grid
.e-rowcell.e-inprogress:not(.e-editedbatchcell):not(.e-updatedtd),
.tailwind-dark
.e-grid
.e-rowcell.e-inprogress:not(.e-editedbatchcell):not(.e-updatedtd),
.material-dark
.e-grid
.e-rowcell.e-inprogress:not(.e-editedbatchcell):not(.e-updatedtd),
.fabric-dark
.e-grid
.e-rowcell.e-inprogress:not(.e-editedbatchcell):not(.e-updatedtd),
.bootstrap-dark
.e-grid
.e-rowcell.e-inprogress:not(.e-editedbatchcell):not(.e-updatedtd) {
background-color: #55241e;
color: #ff9ca0;
}
.material3-dark .e-grid .e-rowcell.e-inprogress,
.material3-dark
.e-grid
.e-rowcell.e-inprogress:not(.e-editedbatchcell):not(.e-updatedtd) {
background-color: #3a1618;
color: #ff7b81;
}
.no-border {
border: none !important;
}
</style>
<template id="ccHeadertemplate"><div>
<span class="e-icons e-columns" id="column-chooser-icon"></span>
<span id="column-chooser-text">Column Options</span>
</div></template>
<template id="ccFootertemplate"><div id="columnChooserFooter">
<button id="submitButton">Apply</button>
<button id="abortButton">Close</button>
</div></template>
<template id="locationTemplate"><div class="image">
<img src="@Url.Content("~/Content/grid/Map.png")" class="e-image" alt=""/>  
<span>${ShipCountry}</span>
</div></template>
@Html.UiGrid(new UiGridOptions
{
Id = "Grid",
Data = ViewData["StackedHeaderData"],
AllowSorting = true,
AllowMultiSorting = true,
AllowFiltering = true,
EnableHover = false,
OnQueryCellInfo = "queryCellInfo",
AllowResizing = true,
AllowPaging = true,
PageSize = 12,
FilterType = UiGridFilterType.Excel,
EditSettings = new UiEditSettings { AllowAdding = true, AllowEditing = true, AllowDeleting = true },
Toolbar = new List<string> { "Add", "Edit", "Delete", "Update", "Cancel", "ColumnChooser" },
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "CustomerID", HeaderText = "Customer ID", Type = UiColumnType.Number, Width = 160, MinWidth = 115, TextAlign = UiTextAlign.Right, IsPrimaryKey = true, ShowInColumnChooser = false, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "CustomerName", HeaderText = "Name", Width = 150, MinWidth = 120 },
new UiGridColumn { HeaderText = "Order Details", TextAlign = UiTextAlign.Center, Columns = new List<UiGridColumn> { new UiGridColumn { Field = "OrderID", Width = 90, Type = UiColumnType.Number, HeaderText = "ID", MinWidth = 90, TextAlign = UiTextAlign.Right }, new UiGridColumn { Field = "OrderDate", Width = 110, Type = UiColumnType.Date, TextAlign = UiTextAlign.Right, HeaderText = "Date", MinWidth = 105, Format = "M/d/yyyy", EditType = UiEditType.DatePickerEdit } } },
new UiGridColumn { HeaderText = "Shipping Details", TextAlign = UiTextAlign.Center, Columns = new List<UiGridColumn> { new UiGridColumn { Field = "ShipCountry", Width = 150, HeaderText = "Country", MinWidth = 120, Template = "#locationTemplate", EditType = UiEditType.DropDownEdit, ValidationRules = new UiValidationRules { Required = true } }, new UiGridColumn { Field = "Freight", Width = 130, Type = UiColumnType.Number, HeaderText = "Charges", TextAlign = UiTextAlign.Right, MinWidth = 115, Format = "C2", EditType = UiEditType.NumericEdit, ValidationRules = new UiValidationRules { Required = true } } } },
new UiGridColumn { HeaderText = "Delivery Details", TextAlign = UiTextAlign.Center, Columns = new List<UiGridColumn> { new UiGridColumn { Field = "Status", Width = 150, HeaderText = "Status", MinWidth = 120, TextAlign = UiTextAlign.Center, EditType = UiEditType.DropDownEdit, ValidationRules = new UiValidationRules { Required = true } }, new UiGridColumn { Field = "Feedback", Width = 130, Type = UiColumnType.Number, HeaderText = "Feedback", AllowResizing = false, MinWidth = 115, TextAlign = UiTextAlign.Center, EditType = UiEditType.NumericEdit, ValidationRules = new UiValidationRules { Required = true, Min = 0, Max = 5 } } } }
}
})
<script>
// Syncfusion colours the Status cell by its value and draws Feedback as a rating; the cell function fills both here.
function queryCellInfo(args) {
if (args.column === 'Status') {
var delivered = args.data.Status === 'Delivered';
args.cell.classList.toggle('e-rowcell-delivered', delivered);
args.cell.classList.toggle('e-rowcell-inprogress', !delivered);
} else if (args.column === 'Feedback') {
var stars = document.createElement('span');
stars.className = 'feedback-stars';
stars.setAttribute('role', 'img');
stars.setAttribute('aria-label', args.data.Feedback + ' of 5');
for (var i = 1; i <= 5; i++) {
var star = document.createElement('span');
star.textContent = '\u2605';
if (i > args.data.Feedback) { star.className = 'off'; }
stars.appendChild(star);
}
args.content = stars;
}
}
</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/StackedHeader.cshtml; it draws what ViewData holds.
public ActionResult StackedHeader()
{
ViewData["StackedHeaderData"] = StackedHeader.GetStackedHeaders(830);
return View();
}
public class StackedHeader
{
public int OrderID { get; set; }
public int CustomerID { get; set; }
public string CustomerName { get; set; }
public DateTime OrderDate { get; set; }
public DateTime ShippedDate { get; set; }
public double Freight { get; set; }
public string ShipName { get; set; }
public string ShipAddress { get; set; }
public string ShipCity { get; set; }
public string ShipRegion { get; set; }
public string ShipCountry { get; set; }
public int Year { get; set; }
public int Salary { get; set; }
public int Feedback { get; set; }
public string Status { get; set; }
public StackedHeader(int orderId, int customerId, string customerName, DateTime orderDate, DateTime shippedDate, double freight, string shipName, string shipAddress, string shipCity, string shipRegion, string shipCountry, int year, int salary, int feedback, string status)
{
this.OrderID = orderId;
this.CustomerID = customerId;
this.CustomerName = customerName;
this.OrderDate = orderDate;
this.ShippedDate = shippedDate;
this.Freight = freight;
this.ShipName = shipName;
this.ShipAddress = shipAddress;
this.ShipCity = shipCity;
this.ShipRegion = shipRegion;
this.ShipCountry = shipCountry;
this.Year = year;
this.Salary = salary;
this.Feedback = feedback;
this.Status = status;
}
public static List<StackedHeader> GetStackedHeaders(long count)
{
string[] customerNames = { "VINET", "TOMSP", "HANAR", "VICTE", "SUPRD", "CHOPS", "RICSU", "WELLI", "HILAA", "ERNSH", "CENTC", "OTTIK", "QUEDE" };
string[] shipCities = { "Reims", "Münster", "Rio de Janeiro", "Lyon", "Charleroi", "Bern", "Genève", "Resende", "San Cristóbal", "Graz", "México D.F.", "Köln", "Rio de Janeiro" };
string[] shipCountries = { "France", "Germany", "Brazil", "Belgium", "Switzerland", "Austria", "Mexico", "Venezuela" };
string[] statusOptions = { "Delivered", "Delivered", "In-progress", "Delivered", "In-progress", "In-progress", "Delivered", "Delivered", "In-progress", "Delivered" };
DateTime[] date = { new DateTime(1704067200000), new DateTime(1706745600000), new DateTime(1730467200000), new DateTime(1727788800000), new DateTime(1725206400000), new DateTime(1722528000000), new DateTime(1719849600000),
new DateTime(1717312000000), new DateTime(1714720000000), new DateTime(1712041600000), new DateTime(1709459200000), new DateTime(1706781000000), new DateTime(1704288000000)};
double[] freightValues = { 10.5, 20.75, 30.0, 40.25, 50.6, 60.85, 70.9, 80.1, 90.45, 100.55 };
int[] feedbackValues = { 1, 2, 3, 4, 5 };
List<StackedHeader> stackedHeader = new List<StackedHeader>();
for (int i = 1; i <= count; i++)
{
stackedHeader.Add(new StackedHeader(
150000 + i,
1000 + i,
customerNames[i % customerNames.Length],
date[i % date.Length],
date[i % date.Length],
freightValues[i % freightValues.Length],
customerNames[i % customerNames.Length] + " Logistics",
"Address " + i,
shipCities[i % shipCities.Length],
null,
shipCountries[i % shipCountries.Length],
2025,
new Random().Next(25000, 55000),
statusOptions[i % statusOptions.Length] == "In-progress" ? 0 : feedbackValues[i % feedbackValues.Length],
statusOptions[i % statusOptions.Length]
));
}
return stackedHeader;
}
}
}
}
@* 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. *@
The Grid allows grouping columns to display multiple levels of headers using the Columns->Columns property.
In this demo, the columns are grouped as follows:
- Order Details: ID and Date
- Shipping Details: Country and Charges
- Delivery Status: Status and Feedback
Column Resizing:
Columns can be resized by clicking and dragging the right edges of the column header. To enable this feature, set AllowResizing to true. To disable resizing for specific columns, set columns->allowResizing to false.
Column Chooser Templates:
The column chooser template is used to customize layout and manage column visibility.
Key properties:
-
ColumnChooserSettings->HeaderTemplate:- Defines the header template. -
ColumnChooserSettings->Template:- Defines the content template. -
ColumnChooserSettings->FooterTemplate:- Defines the footer template. -
ColumnChooserSettings->EnableSearching:- Enables or disables search functionality.
In this demo, the showInColumnChooser of the Customer ID column is set to false, preventing it from being displayed in the column chooser popup.