Example of Group and Caption Aggregate in ASP.NET Core Data Grid Control
This sample demonstrates Aggregate functionality of the Grid. In this sample, “Energy Produced” column will displays their sum aggregate value in group footer, as well as the min and max aggregate value in group caption.
@Html.UiGrid(new UiGridOptions
{
Id = "CaptionAggregate",
Data = ViewData["datasource"],
GridLines = UiGridLines.Vertical,
AllowGrouping = true,
Height = "300px",
EnableHover = false,
AllowSorting = true,
AllowMultiSorting = true,
AllowFiltering = true,
FilterType = UiGridFilterType.Excel,
GroupSettings = new UiGroupSettings { ShowGroupedColumn = true, ShowDropArea = false, Columns = new[] { "ConsumptionCategory" } },
Aggregates = new List<UiGridAggregateRow>
{
new UiGridAggregateRow
{
Columns = new List<UiGridAggregateColumn>
{
new UiGridAggregateColumn { Field = "EnergyProduced", Type = UiAggregateType.Sum, FooterTemplate = "Total Energy Produced: ${Sum} KWh" },
new UiGridAggregateColumn { Field = "EnergyProduced", Type = UiAggregateType.Sum, GroupFooterTemplate = "Total Energy Produced: ${Sum} KWh" },
new UiGridAggregateColumn { Field = "EnergyProduced", Type = UiAggregateType.Max, GroupCaptionTemplate = "#aggGroupCaptionEnergyProducedMax" }
}
},
new UiGridAggregateRow
{
Columns = new List<UiGridAggregateColumn>
{
new UiGridAggregateColumn { Field = "EnergyProduced", Type = UiAggregateType.Average, FooterTemplate = "Average Energy Produced: ${Average} KWh" }
}
}
},
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "ID", IsPrimaryKey = true, Type = UiColumnType.Number, Visible = false },
new UiGridColumn { Field = "Month", HeaderText = "Month", Type = UiColumnType.Date, Width = 120, TextAlign = UiTextAlign.Left, ClipMode = UiClipMode.EllipsisWithTooltip, Format = "M/d/yyyy" },
new UiGridColumn { Field = "ConsumptionCategory", HeaderText = "Category", Width = 130, TextAlign = UiTextAlign.Left, ClipMode = UiClipMode.EllipsisWithTooltip },
new UiGridColumn { TextAlign = UiTextAlign.Center, HeaderTemplate = "#energyTemplate", Columns = new List<UiGridColumn> { new UiGridColumn { Field = "EnergyConsumed", Width = 150, Type = UiColumnType.Number, HeaderText = "Consumed", TextAlign = UiTextAlign.Right, ClipMode = UiClipMode.EllipsisWithTooltip }, new UiGridColumn { Field = "EnergyProduced", Width = 300, Type = UiColumnType.Number, HeaderText = "Produced", TextAlign = UiTextAlign.Right } } },
new UiGridColumn { Field = "WeatherCondition", HeaderText = "Weather", Width = 120, TextAlign = UiTextAlign.Left, ClipMode = UiClipMode.EllipsisWithTooltip },
new UiGridColumn { Field = "EnergyPrice", HeaderText = "Energy", Type = UiColumnType.Number, Width = 130, TextAlign = UiTextAlign.Right, ClipMode = UiClipMode.EllipsisWithTooltip, Format = "C2" }
}
})
<style>
.e-maximum{
color:#008000;
font-weight: bold;
}
.e-minimum{
color:#dc3545;
font-weight: bold;
}
.bootstrap5 .e-grid .e-icon.energy.e-icons::before,
.bootstrap5-dark .e-grid .e-icon.energy.e-icons::before {
content: '\e8cd';
}
.highcontrast .e-grid .e-icon.energy.e-icons::before {
content: '\e991';
}
.tailwind .e-grid .e-icon.energy.e-icons::before,
.tailwind-dark .e-grid .e-icon.energy.e-icons::before,
.fluent .e-grid .e-icon.energy.e-icons::before,
.fluent-dark .e-grid .e-icon.energy.e-icons::before,
.fluent2 .e-grid .e-icon.energy.e-icons::before,
.fluent2-dark .e-grid .e-icon.energy.e-icons::before,
.fluent2-highcontrast .e-grid .e-icon.energy.e-icons::before,
.material3 .e-grid .e-icon.energy.e-icons::before,
.material3-dark .e-grid .e-icon.energy.e-icons::before {
content: '\e8cd';
}
#CaptionAggregate.e-grid tr.e-altrow .e-rowcell:not(.e-selectionbackground.e-active) {
background-color: rgb(228 228 228 / 15%);
}
</style>
<template id="energyTemplate"><span class="energy e-icons e-icon"></span>
<span> Energy (KWh)</span></template>
<template id="aggGroupCaptionEnergyProducedMax"><div><span class='e-maximum'> Max : ${Max}</span></div></template>
@Html.UiGrid(new UiGridOptions
{
Id = "CaptionAggregate",
Data = ViewData["datasource"],
GridLines = UiGridLines.Vertical,
AllowGrouping = true,
Height = "300px",
EnableHover = false,
AllowSorting = true,
AllowMultiSorting = true,
AllowFiltering = true,
FilterType = UiGridFilterType.Excel,
GroupSettings = new UiGroupSettings { ShowGroupedColumn = true, ShowDropArea = false, Columns = new[] { "ConsumptionCategory" } },
Aggregates = new List<UiGridAggregateRow>
{
new UiGridAggregateRow
{
Columns = new List<UiGridAggregateColumn>
{
new UiGridAggregateColumn { Field = "EnergyProduced", Type = UiAggregateType.Sum, FooterTemplate = "Total Energy Produced: ${Sum} KWh" },
new UiGridAggregateColumn { Field = "EnergyProduced", Type = UiAggregateType.Sum, GroupFooterTemplate = "Total Energy Produced: ${Sum} KWh" },
new UiGridAggregateColumn { Field = "EnergyProduced", Type = UiAggregateType.Max, GroupCaptionTemplate = "#aggGroupCaptionEnergyProducedMax" }
}
},
new UiGridAggregateRow
{
Columns = new List<UiGridAggregateColumn>
{
new UiGridAggregateColumn { Field = "EnergyProduced", Type = UiAggregateType.Average, FooterTemplate = "Average Energy Produced: ${Average} KWh" }
}
}
},
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "ID", IsPrimaryKey = true, Type = UiColumnType.Number, Visible = false },
new UiGridColumn { Field = "Month", HeaderText = "Month", Type = UiColumnType.Date, Width = 120, TextAlign = UiTextAlign.Left, ClipMode = UiClipMode.EllipsisWithTooltip, Format = "M/d/yyyy" },
new UiGridColumn { Field = "ConsumptionCategory", HeaderText = "Category", Width = 130, TextAlign = UiTextAlign.Left, ClipMode = UiClipMode.EllipsisWithTooltip },
new UiGridColumn { TextAlign = UiTextAlign.Center, HeaderTemplate = "#energyTemplate", Columns = new List<UiGridColumn> { new UiGridColumn { Field = "EnergyConsumed", Width = 150, Type = UiColumnType.Number, HeaderText = "Consumed", TextAlign = UiTextAlign.Right, ClipMode = UiClipMode.EllipsisWithTooltip }, new UiGridColumn { Field = "EnergyProduced", Width = 300, Type = UiColumnType.Number, HeaderText = "Produced", TextAlign = UiTextAlign.Right } } },
new UiGridColumn { Field = "WeatherCondition", HeaderText = "Weather", Width = 120, TextAlign = UiTextAlign.Left, ClipMode = UiClipMode.EllipsisWithTooltip },
new UiGridColumn { Field = "EnergyPrice", HeaderText = "Energy", Type = UiColumnType.Number, Width = 130, TextAlign = UiTextAlign.Right, ClipMode = UiClipMode.EllipsisWithTooltip, Format = "C2" }
}
})
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/GroupCaptionAggregate.cshtml; it draws what ViewData holds.
public ActionResult GroupCaptionAggregate()
{
var DataSource = EnergyData.GetAllRecords();
ViewData["dataSource"] = DataSource;
return View();
}
public class EnergyData
{
public EnergyData()
{
}
public EnergyData(int ID, DateTime Month, int EnergyProduced, int EnergyConsumed, string ConsumptionCategory, string WeatherCondition, int EnergyPrice)
{
this.ID = ID;
this.Month = Month;
this.EnergyProduced = EnergyProduced;
this.EnergyConsumed = EnergyConsumed;
this.ConsumptionCategory = ConsumptionCategory;
this.WeatherCondition = WeatherCondition;
this.EnergyPrice = EnergyPrice;
}
public int ID { get; set; }
public DateTime Month { get; set; }
public int EnergyProduced { get; set; }
public int EnergyConsumed { get; set; }
public string ConsumptionCategory { get; set; }
public int EnergyPrice { get; set; }
public string WeatherCondition {get; set; }
public static List<EnergyData> GetAllRecords()
{
List<EnergyData> energyDataList = new List<EnergyData>();
energyDataList.Add(new EnergyData(1, new DateTime(1704067200000), 3900, 3400, "Residential", "Snowy", 52));
energyDataList.Add(new EnergyData(2, new DateTime(1704067200000), 7400, 6800, "Commercial", "Snowy", 57));
energyDataList.Add(new EnergyData(3, new DateTime(1704067200000), 11400, 10200, "Industrial", "Snowy", 62));
energyDataList.Add(new EnergyData(4, new DateTime(1706745600000), 3700, 3200, "Residential", "Cloudy", 54));
energyDataList.Add(new EnergyData(5, new DateTime(1706745600000), 7000, 7400, "Commercial", "Cloudy", 59));
energyDataList.Add(new EnergyData(6, new DateTime(1706745600000), 10600, 10000, "Industrial", "Cloudy", 64));
energyDataList.Add(new EnergyData(7, new DateTime(1709232000000), 4000, 3500, "Residential", "Sunny", 56));
energyDataList.Add(new EnergyData(8, new DateTime(1709232000000), 7600, 8000, "Commercial", "Sunny", 60));
energyDataList.Add(new EnergyData(9, new DateTime(1709232000000), 11500, 10500, "Industrial", "Sunny", 65));
energyDataList.Add(new EnergyData(10, new DateTime(1711910400000), 4100, 4200, "Residential", "Rainy", 57));
energyDataList.Add(new EnergyData(11, new DateTime(1711910400000), 7800, 7200, "Commercial", "Rainy", 62));
energyDataList.Add(new EnergyData(12, new DateTime(1711910400000), 11900, 11800, "Industrial", "Rainy", 67));
energyDataList.Add(new EnergyData(13, new DateTime(1714588800000), 4200, 3700, "Residential", "Sunny", 58));
energyDataList.Add(new EnergyData(14, new DateTime(1714588800000), 8000, 8500, "Commercial", "Sunny", 63));
energyDataList.Add(new EnergyData(15, new DateTime(1714588800000), 12200, 11000, "Industrial", "Sunny", 69));
energyDataList.Add(new EnergyData(16, new DateTime(1717267200000), 4300, 3800, "Residential", "Cloudy", 59));
energyDataList.Add(new EnergyData(17, new DateTime(1717267200000), 8200, 7600, "Commercial", "Cloudy", 64));
energyDataList.Add(new EnergyData(18, new DateTime(1717267200000), 12700, 11500, "Industrial", "Cloudy", 70));
energyDataList.Add(new EnergyData(19, new DateTime(1719849600000), 4400, 3900, "Residential", "Hot", 61));
energyDataList.Add(new EnergyData(20, new DateTime(1719849600000), 8400, 7800, "Commercial", "Hot", 66));
energyDataList.Add(new EnergyData(21, new DateTime(1719849600000), 13300, 12000, "Industrial", "Hot", 73));
energyDataList.Add(new EnergyData(22, new DateTime(1722528000000), 4500, 5000, "Residential", "Snowy", 63));
energyDataList.Add(new EnergyData(23, new DateTime(1722528000000), 8600, 8000, "Commercial", "Snowy", 68));
energyDataList.Add(new EnergyData(24, new DateTime(1722528000000), 13900, 12500, "Industrial", "Snowy", 76));
energyDataList.Add(new EnergyData(25, new DateTime(1725206400000), 4600, 5000, "Residential", "Hot", 64));
energyDataList.Add(new EnergyData(26, new DateTime(1725206400000), 8800, 8200, "Commercial", "Hot", 69));
energyDataList.Add(new EnergyData(27, new DateTime(1725206400000), 14400, 13000, "Industrial", "Hot", 79));
energyDataList.Add(new EnergyData(28, new DateTime(1727788800000), 4700, 5000, "Residential", "Rainy", 65));
energyDataList.Add(new EnergyData(29, new DateTime(1727788800000), 9000, 8400, "Commercial", "Rainy", 70));
energyDataList.Add(new EnergyData(30, new DateTime(1727788800000), 14900, 13500, "Industrial", "Rainy", 82));
energyDataList.Add(new EnergyData(31, new DateTime(1730467200000), 4800, 4300, "Residential", "Cloudy", 66));
energyDataList.Add(new EnergyData(32, new DateTime(1730467200000), 9200, 8600, "Commercial", "Cloudy", 72));
energyDataList.Add(new EnergyData(33, new DateTime(1730467200000), 15400, 14000, "Industrial", "Cloudy", 85));
energyDataList.Add(new EnergyData(34, new DateTime(1733145600000), 4900, 5000, "Residential", "Hot", 67));
energyDataList.Add(new EnergyData(35, new DateTime(1733145600000), 9400, 8800, "Commercial", "Hot", 74));
energyDataList.Add(new EnergyData(36, new DateTime(1733145600000), 15900, 14500, "Industrial", "Hot", 88));
return energyDataList;
}
}
}
}
@* 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 supports displaying aggregates in the footer, group footer and group caption. The aggregate configurations can be provided by the Aggregates property.
The built-in aggregates are,
SumAverageMinMaxCountTrueCountFalseCountCustom- Requires theCustomAggregateproperty to perform the aggregation. The custom aggregate value can be accessed inside template using the key${Custom}
In this demo, the GroupFooterTemplate property is used to display the group footer aggregation for the Energy Produced column and GroupCaptionTemplate property is used to display the group caption aggregation.
To enable group footer aggregation for Energy Produced column, the Sum aggregate type is used by setting the type and field property to EnergyProduced, which will be used to perform the aggregation. The aggregate value is accessed inside the groupCaptionTemplate using its Type name (${Sum}).
To enable group caption aggregation for Energy Produced column, the Max aggregate type is used by setting the Type and Field property is set as EnergyProduced which will be used to perform the aggregation. The aggregate value is accessed inside theGroupCaptionTemplate using its Type name (${Max}).
In this sample, GroupCaptionTemplate to show the multiple aggregate values.
The template expression should be provided inside ${...} the interpolation syntax.