Example of Default Aggregate in ASP.NET Core Data Grid Control
This sample demonstrates aggregate functionality of the Grid. In this sample, the aggregate value for the columns are displayed in column footer.
@Html.UiGrid(new UiGridOptions
{
Id = "DefaultAggregate",
Data = ViewData["dataSource"],
GridLines = UiGridLines.Vertical,
Height = "300px",
EnableHover = false,
AllowMultiSorting = true,
AllowSorting = true,
AllowFiltering = true,
PageCount = 5,
FilterType = UiGridFilterType.Excel,
Aggregates = new List<UiGridAggregateRow>
{
new UiGridAggregateRow
{
Columns = new List<UiGridAggregateColumn>
{
new UiGridAggregateColumn { Field = "Sales", Type = UiAggregateType.Sum, Format = "C2", FooterTemplate = "Total: ${Sum}" },
new UiGridAggregateColumn { Field = "MarketingSpend", Type = UiAggregateType.Sum, Format = "C2", FooterTemplate = "Total: ${Sum}" },
new UiGridAggregateColumn { Field = "ReturningCustomers", Type = UiAggregateType.Sum, Format = "N", FooterTemplate = "Returning Customers: ${Sum}" },
new UiGridAggregateColumn { Field = "NewCustomers", Type = UiAggregateType.Sum, Format = "N", FooterTemplate = "New Customers: ${Sum}" }
}
},
new UiGridAggregateRow
{
Columns = new List<UiGridAggregateColumn>
{
new UiGridAggregateColumn { Field = "Sales", Type = UiAggregateType.Average, Format = "C2", FooterTemplate = "Average: ${Average}" },
new UiGridAggregateColumn { Field = "MarketingSpend", Type = UiAggregateType.Average, Format = "C2", FooterTemplate = "Total: ${Average}" },
new UiGridAggregateColumn { Field = "WebTraffic", Type = UiAggregateType.Max, Format = "N", FooterTemplate = "Total: ${Max}" }
}
},
new UiGridAggregateRow
{
Columns = new List<UiGridAggregateColumn>
{
new UiGridAggregateColumn { Field = "WebTraffic", Type = UiAggregateType.Min, Format = "N", FooterTemplate = "Total: ${Min}" }
}
}
},
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "Month", HeaderText = "TimeStamp", Width = 140, TextAlign = UiTextAlign.Left, ClipMode = UiClipMode.EllipsisWithTooltip, IsPrimaryKey = true },
new UiGridColumn { Field = "Sales", HeaderText = "Sales", Type = UiColumnType.Number, Width = 150, TextAlign = UiTextAlign.Right, Format = "C2" },
new UiGridColumn { Field = "MarketingSpend", HeaderText = "Marketing Spend", Type = UiColumnType.Number, Width = 190, TextAlign = UiTextAlign.Right, ClipMode = UiClipMode.EllipsisWithTooltip, Format = "C2" },
new UiGridColumn { Field = "NewCustomers", HeaderText = "New Customers", Type = UiColumnType.Number, Width = 180, TextAlign = UiTextAlign.Right, ClipMode = UiClipMode.EllipsisWithTooltip },
new UiGridColumn { Field = "ReturningCustomers", HeaderText = "Returning Customers", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, Width = 220, ClipMode = UiClipMode.EllipsisWithTooltip },
new UiGridColumn { Field = "WebTraffic", HeaderText = "Web Traffic", Type = UiColumnType.Number, Width = 160, TextAlign = UiTextAlign.Right, ClipMode = UiClipMode.EllipsisWithTooltip }
}
})
<style>
#DefaultAggregate.e-grid tr.e-altrow .e-rowcell:not(.e-selectionbackground.e-active) {
background-color: rgb(228 228 228 / 15%);
}
</style>
@Html.UiGrid(new UiGridOptions
{
Id = "DefaultAggregate",
Data = ViewData["dataSource"],
GridLines = UiGridLines.Vertical,
Height = "300px",
EnableHover = false,
AllowMultiSorting = true,
AllowSorting = true,
AllowFiltering = true,
PageCount = 5,
FilterType = UiGridFilterType.Excel,
Aggregates = new List<UiGridAggregateRow>
{
new UiGridAggregateRow
{
Columns = new List<UiGridAggregateColumn>
{
new UiGridAggregateColumn { Field = "Sales", Type = UiAggregateType.Sum, Format = "C2", FooterTemplate = "Total: ${Sum}" },
new UiGridAggregateColumn { Field = "MarketingSpend", Type = UiAggregateType.Sum, Format = "C2", FooterTemplate = "Total: ${Sum}" },
new UiGridAggregateColumn { Field = "ReturningCustomers", Type = UiAggregateType.Sum, Format = "N", FooterTemplate = "Returning Customers: ${Sum}" },
new UiGridAggregateColumn { Field = "NewCustomers", Type = UiAggregateType.Sum, Format = "N", FooterTemplate = "New Customers: ${Sum}" }
}
},
new UiGridAggregateRow
{
Columns = new List<UiGridAggregateColumn>
{
new UiGridAggregateColumn { Field = "Sales", Type = UiAggregateType.Average, Format = "C2", FooterTemplate = "Average: ${Average}" },
new UiGridAggregateColumn { Field = "MarketingSpend", Type = UiAggregateType.Average, Format = "C2", FooterTemplate = "Total: ${Average}" },
new UiGridAggregateColumn { Field = "WebTraffic", Type = UiAggregateType.Max, Format = "N", FooterTemplate = "Total: ${Max}" }
}
},
new UiGridAggregateRow
{
Columns = new List<UiGridAggregateColumn>
{
new UiGridAggregateColumn { Field = "WebTraffic", Type = UiAggregateType.Min, Format = "N", FooterTemplate = "Total: ${Min}" }
}
}
},
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "Month", HeaderText = "TimeStamp", Width = 140, TextAlign = UiTextAlign.Left, ClipMode = UiClipMode.EllipsisWithTooltip, IsPrimaryKey = true },
new UiGridColumn { Field = "Sales", HeaderText = "Sales", Type = UiColumnType.Number, Width = 150, TextAlign = UiTextAlign.Right, Format = "C2" },
new UiGridColumn { Field = "MarketingSpend", HeaderText = "Marketing Spend", Type = UiColumnType.Number, Width = 190, TextAlign = UiTextAlign.Right, ClipMode = UiClipMode.EllipsisWithTooltip, Format = "C2" },
new UiGridColumn { Field = "NewCustomers", HeaderText = "New Customers", Type = UiColumnType.Number, Width = 180, TextAlign = UiTextAlign.Right, ClipMode = UiClipMode.EllipsisWithTooltip },
new UiGridColumn { Field = "ReturningCustomers", HeaderText = "Returning Customers", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, Width = 220, ClipMode = UiClipMode.EllipsisWithTooltip },
new UiGridColumn { Field = "WebTraffic", HeaderText = "Web Traffic", Type = UiColumnType.Number, Width = 160, TextAlign = UiTextAlign.Right, ClipMode = UiClipMode.EllipsisWithTooltip }
}
})
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/DefaultAggregate.cshtml; it draws what ViewData holds.
public ActionResult DefaultAggregate()
{
var order = OverAllData.GetAllRecords();
ViewData["datasource"] = order;
return View();
}
public class OverAllData
{
public OverAllData()
{
}
public OverAllData(string Month, int Sales, int MarketingSpend, int NewCustomers, int ReturningCustomers, int WebTraffic)
{
this.Month = Month;
this.Sales = Sales;
this.MarketingSpend = MarketingSpend;
this.NewCustomers = NewCustomers;
this.ReturningCustomers = ReturningCustomers;
this.WebTraffic = WebTraffic;
}
public string Month { get; set; }
public int Sales { get; set; }
public int MarketingSpend { get; set; }
public int NewCustomers { get; set; }
public int ReturningCustomers { get; set; }
public int WebTraffic {get; set; }
public static List<OverAllData> GetAllRecords()
{
int previousYear = DateTime.Now.Year - 1;
List<OverAllData> OverAllDataList = new List<OverAllData>();
OverAllDataList.Add(new OverAllData("January " + previousYear, 51000, 9000, 180, 150, 200));
OverAllDataList.Add(new OverAllData("February " + previousYear, 46000, 9200, 190, 160, 320));
OverAllDataList.Add(new OverAllData("March " + previousYear, 45000, 9400, 200, 155, 190));
OverAllDataList.Add(new OverAllData("April " + previousYear, 48000, 9600, 210, 165, 100));
OverAllDataList.Add(new OverAllData("May " + previousYear, 49000, 9800, 220, 170, 230));
OverAllDataList.Add(new OverAllData("June " + previousYear, 52000, 9600, 210, 160, 300));
OverAllDataList.Add(new OverAllData("July " + previousYear, 48000, 9700, 215, 170, 175));
OverAllDataList.Add(new OverAllData("August " + previousYear, 50000, 9800, 225, 180, 190));
OverAllDataList.Add(new OverAllData("September " + previousYear, 45000, 9700, 220, 175, 120));
OverAllDataList.Add(new OverAllData("October " + previousYear, 46000, 10000, 230, 190, 160));
OverAllDataList.Add(new OverAllData("November " + previousYear, 50000, 9900, 225, 185, 230));
OverAllDataList.Add(new OverAllData("December " + previousYear, 47000, 10200, 240, 200, 145));
return OverAllDataList;
}
}
}
}
@* 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 its footer, group footer and group caption. The aggregate configurations can be provided by the Aggregates property.
The built-in aggregates are,
SumAverageMinMaxCountTruecountFalsecountCustom
In this demo, the FooterTemplate property is used to display four different aggregates (Sum, Average, Max, Min) in the Grid footer. Each aggregate type is specified using the type and field properties accessed in the footer template by its type name. The aggregate value will be formatted based on its format value(N2 or C2) before being displayed.