Example of Infinite Scrolling in ASP.NET Core Data Grid Control
This sample showcases the infinite scrolling capability of the Grid, designed to handle large datasets seamlessly. Click the “Load 100K Data” button to populate the Grid with data, then scroll vertically and horizontally to dynamically load rows and columns, ensuring smooth navigation.
@Html.UiGrid(new UiGridOptions
{
Id = "InfiniteScroll",
Data = new List<object>(),
Height = "400px",
OnDataLoaded = "hide",
AllowSorting = true,
AllowFiltering = true,
EnableInfiniteScrolling = true,
EnableColumnVirtualization = true,
RowHeight = 40,
FilterType = UiGridFilterType.CheckBox,
Aggregates = new List<UiGridAggregateRow>
{
new UiGridAggregateRow
{
Columns = new List<UiGridAggregateColumn>
{
new UiGridAggregateColumn { Field = "SalesQty", Type = UiAggregateType.Sum, Format = "N0", FooterTemplate = "${Sum}" },
new UiGridAggregateColumn { Field = "GrossAmount", Type = UiAggregateType.Sum, Format = "C0", FooterTemplate = "${Sum}" },
new UiGridAggregateColumn { Field = "NetAmount", Type = UiAggregateType.Sum, Format = "C0", FooterTemplate = "${Sum}" },
new UiGridAggregateColumn { Field = "ShippingCost", Type = UiAggregateType.Sum, Format = "C0", FooterTemplate = "${Sum}" },
new UiGridAggregateColumn { Field = "Profit", Type = UiAggregateType.Sum, Format = "C0", FooterTemplate = "${Sum}" },
new UiGridAggregateColumn { Field = "Forecast", Type = UiAggregateType.Sum, Format = "C0", FooterTemplate = "${Sum}" },
new UiGridAggregateColumn { Field = "ReturnQty", Type = UiAggregateType.Sum, Format = "N0", FooterTemplate = "${Sum}" },
new UiGridAggregateColumn { Field = "ReturnAmount", Type = UiAggregateType.Sum, Format = "C0", FooterTemplate = "${Sum}" }
}
}
},
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "ProductId", HeaderText = "Product ID", TextAlign = UiTextAlign.Right, Width = 130 },
new UiGridColumn { Field = "ProductName", HeaderText = "Product Name", Width = 210 },
new UiGridColumn { Field = "GrossAmount", HeaderText = "Gross Amount", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, Width = 180, Format = "C2" },
new UiGridColumn { Field = "NetAmount", HeaderText = "Net Amount", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, Width = 180, Format = "C2" },
new UiGridColumn { Field = "ProfitMargin", HeaderText = "Profit (%)", Width = 180 },
new UiGridColumn { Field = "AchievementPercent", HeaderText = "Achievement (%)", Width = 190 },
new UiGridColumn { Field = "SalesQty", HeaderText = "Sales Quantity", TextAlign = UiTextAlign.Right, Width = 150 },
new UiGridColumn { Field = "UnitPrice", HeaderText = "Unit Price", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, Width = 120, Format = "C2" },
new UiGridColumn { Field = "Month", HeaderText = "Month", Width = 120 },
new UiGridColumn { Field = "Category", HeaderText = "Category", Width = 130 },
new UiGridColumn { Field = "SubCategory", HeaderText = "Sub Category", Width = 150, Visible = false },
new UiGridColumn { Field = "Brand", HeaderText = "Brand", Width = 120 },
new UiGridColumn { Field = "City", HeaderText = "City", Width = 130 },
new UiGridColumn { Field = "State", HeaderText = "State", Width = 120 },
new UiGridColumn { Field = "Country", HeaderText = "Country", Width = 160 },
new UiGridColumn { Field = "Region", HeaderText = "Region", Width = 130 },
new UiGridColumn { Field = "Discount", HeaderText = "Discount (%)", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, Width = 140, Format = "N0" },
new UiGridColumn { Field = "Tax", HeaderText = "Tax (%)", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, Width = 130, Format = "N2" },
new UiGridColumn { Field = "ShippingCost", HeaderText = "Shipping Cost", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, Width = 160, Format = "C2" },
new UiGridColumn { Field = "Profit", HeaderText = "Profit", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, Width = 160, Format = "C2" },
new UiGridColumn { Field = "Target", HeaderText = "Target", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, Width = 120, Format = "C2" },
new UiGridColumn { Field = "Forecast", HeaderText = "Forecast", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, Width = 150, Format = "C2" },
new UiGridColumn { Field = "SalesRep", HeaderText = "Sales Reporter", Width = 150 },
new UiGridColumn { Field = "Manager", HeaderText = "Manager", Width = 140 },
new UiGridColumn { Field = "Channel", HeaderText = "Channel", Width = 120 },
new UiGridColumn { Field = "Quarter", HeaderText = "Quarter", Width = 120 },
new UiGridColumn { Field = "Year", HeaderText = "Year", TextAlign = UiTextAlign.Right, Width = 100 },
new UiGridColumn { Field = "ReturnQty", HeaderText = "Return Quantity", TextAlign = UiTextAlign.Right, Width = 160 },
new UiGridColumn { Field = "ReturnAmount", HeaderText = "Return Amount", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, Width = 160, Format = "C2" },
new UiGridColumn { Field = "Remarks", HeaderText = "Remarks", Width = 220 }
}
})
<style>
#popup { display: none; margin-left: 8px; }
.div-button { margin: 5px 5px 5px 0; }
</style>
<div style="padding: 15px"><button type="button" id="generate">Load 100K Data</button><span id="popup" role="status">Loading...</span></div>
@Html.UiGrid(new UiGridOptions
{
Id = "InfiniteScroll",
Data = new List<object>(),
Height = "400px",
OnDataLoaded = "hide",
AllowSorting = true,
AllowFiltering = true,
EnableInfiniteScrolling = true,
EnableColumnVirtualization = true,
RowHeight = 40,
FilterType = UiGridFilterType.CheckBox,
Aggregates = new List<UiGridAggregateRow>
{
new UiGridAggregateRow
{
Columns = new List<UiGridAggregateColumn>
{
new UiGridAggregateColumn { Field = "SalesQty", Type = UiAggregateType.Sum, Format = "N0", FooterTemplate = "${Sum}" },
new UiGridAggregateColumn { Field = "GrossAmount", Type = UiAggregateType.Sum, Format = "C0", FooterTemplate = "${Sum}" },
new UiGridAggregateColumn { Field = "NetAmount", Type = UiAggregateType.Sum, Format = "C0", FooterTemplate = "${Sum}" },
new UiGridAggregateColumn { Field = "ShippingCost", Type = UiAggregateType.Sum, Format = "C0", FooterTemplate = "${Sum}" },
new UiGridAggregateColumn { Field = "Profit", Type = UiAggregateType.Sum, Format = "C0", FooterTemplate = "${Sum}" },
new UiGridAggregateColumn { Field = "Forecast", Type = UiAggregateType.Sum, Format = "C0", FooterTemplate = "${Sum}" },
new UiGridAggregateColumn { Field = "ReturnQty", Type = UiAggregateType.Sum, Format = "N0", FooterTemplate = "${Sum}" },
new UiGridAggregateColumn { Field = "ReturnAmount", Type = UiAggregateType.Sum, Format = "C0", FooterTemplate = "${Sum}" }
}
}
},
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "ProductId", HeaderText = "Product ID", TextAlign = UiTextAlign.Right, Width = 130 },
new UiGridColumn { Field = "ProductName", HeaderText = "Product Name", Width = 210 },
new UiGridColumn { Field = "GrossAmount", HeaderText = "Gross Amount", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, Width = 180, Format = "C2" },
new UiGridColumn { Field = "NetAmount", HeaderText = "Net Amount", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, Width = 180, Format = "C2" },
new UiGridColumn { Field = "ProfitMargin", HeaderText = "Profit (%)", Width = 180 },
new UiGridColumn { Field = "AchievementPercent", HeaderText = "Achievement (%)", Width = 190 },
new UiGridColumn { Field = "SalesQty", HeaderText = "Sales Quantity", TextAlign = UiTextAlign.Right, Width = 150 },
new UiGridColumn { Field = "UnitPrice", HeaderText = "Unit Price", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, Width = 120, Format = "C2" },
new UiGridColumn { Field = "Month", HeaderText = "Month", Width = 120 },
new UiGridColumn { Field = "Category", HeaderText = "Category", Width = 130 },
new UiGridColumn { Field = "SubCategory", HeaderText = "Sub Category", Width = 150, Visible = false },
new UiGridColumn { Field = "Brand", HeaderText = "Brand", Width = 120 },
new UiGridColumn { Field = "City", HeaderText = "City", Width = 130 },
new UiGridColumn { Field = "State", HeaderText = "State", Width = 120 },
new UiGridColumn { Field = "Country", HeaderText = "Country", Width = 160 },
new UiGridColumn { Field = "Region", HeaderText = "Region", Width = 130 },
new UiGridColumn { Field = "Discount", HeaderText = "Discount (%)", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, Width = 140, Format = "N0" },
new UiGridColumn { Field = "Tax", HeaderText = "Tax (%)", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, Width = 130, Format = "N2" },
new UiGridColumn { Field = "ShippingCost", HeaderText = "Shipping Cost", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, Width = 160, Format = "C2" },
new UiGridColumn { Field = "Profit", HeaderText = "Profit", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, Width = 160, Format = "C2" },
new UiGridColumn { Field = "Target", HeaderText = "Target", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, Width = 120, Format = "C2" },
new UiGridColumn { Field = "Forecast", HeaderText = "Forecast", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, Width = 150, Format = "C2" },
new UiGridColumn { Field = "SalesRep", HeaderText = "Sales Reporter", Width = 150 },
new UiGridColumn { Field = "Manager", HeaderText = "Manager", Width = 140 },
new UiGridColumn { Field = "Channel", HeaderText = "Channel", Width = 120 },
new UiGridColumn { Field = "Quarter", HeaderText = "Quarter", Width = 120 },
new UiGridColumn { Field = "Year", HeaderText = "Year", TextAlign = UiTextAlign.Right, Width = 100 },
new UiGridColumn { Field = "ReturnQty", HeaderText = "Return Quantity", TextAlign = UiTextAlign.Right, Width = 160 },
new UiGridColumn { Field = "ReturnAmount", HeaderText = "Return Amount", Type = UiColumnType.Number, TextAlign = UiTextAlign.Right, Width = 160, Format = "C2" },
new UiGridColumn { Field = "Remarks", HeaderText = "Remarks", Width = 220 }
}
})
<script>
var salesDataSource = [], flag = true;
function createSalesDataSource() {
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var productCatalog = [
{
name: 'Smart Pro Laptop',
category: 'Electronics',
subCategory: 'Computing',
brand: 'Apex Tech',
basePrice: 1800,
country: 'USA',
state: 'California',
city: 'San Jose',
region: 'North America'
},
{
name: 'Ergo Desk Monitor',
category: 'Electronics',
subCategory: 'Displays',
brand: 'Vision Core',
basePrice: 780,
country: 'Germany',
state: 'Bavaria',
city: 'Munich',
region: 'Europe'
},
{
name: 'Office Chair X',
category: 'Furniture',
subCategory: 'Seating',
brand: 'Comfort Line',
basePrice: 450,
country: 'Singapore',
state: 'Central',
city: 'Singapore',
region: 'Asia'
},
{
name: 'Wireless Keyboard',
category: 'Accessories',
subCategory: 'Input Devices',
brand: 'Key Wave',
basePrice: 200,
country: 'USA',
state: 'Texas',
city: 'Austin',
region: 'North America'
},
{
name: '4K Business Display',
category: 'Electronics',
subCategory: 'Displays',
brand: 'Vision Core',
basePrice: 1400,
country: 'Brazil',
state: 'São Paulo',
city: 'São Paulo',
region: 'South America'
},
{
name: 'Ergonomic Mouse Pro',
category: 'Accessories',
subCategory: 'Input Devices',
brand: 'Key Wave',
basePrice: 90,
country: 'United Kingdom',
state: 'England',
city: 'London',
region: 'Europe'
},
{
name: 'Conference Speaker Set',
category: 'Audio',
subCategory: 'Conference Gear',
brand: 'Sound Sphere',
basePrice: 750,
country: 'Australia',
state: 'NSW',
city: 'Sydney',
region: 'Oceania'
},
{
name: 'Cloud Backup License',
category: 'Software',
subCategory: 'Subscription',
brand: 'Cloud Nexus',
basePrice: 300,
country: 'Canada',
state: 'Ontario',
city: 'Toronto',
region: 'North America'
},
{
name: 'Smart Docking Hub',
category: 'Electronics',
subCategory: 'Accessories',
brand: 'Apex Tech',
basePrice: 260,
country: 'Netherlands',
state: 'North Holland',
city: 'Amsterdam',
region: 'Europe'
},
{
name: 'Premium Standing Desk',
category: 'Furniture',
subCategory: 'Desks',
brand: 'Comfort Line',
basePrice: 950,
country: 'USA',
state: 'Washington',
city: 'Seattle',
region: 'North America'
},
{
name: 'Noise Cancel Headset',
category: 'Audio',
subCategory: 'Headphones',
brand: 'Sound Sphere',
basePrice: 220,
country: 'Japan',
state: 'Tokyo',
city: 'Tokyo',
region: 'Asia'
},
{
name: 'Security Suite License',
category: 'Software',
subCategory: 'License',
brand: 'Cloud Nexus',
basePrice: 480,
country: 'India',
state: 'Karnataka',
city: 'Bengaluru',
region: 'Asia'
},
];
var salesReps = ['Maya Chen', 'Lena Fischer', 'Arun Patel', 'Noah Davis', 'Camila Souza', 'Emma Clark', 'Ethan Brown', 'Sophia Martin', 'Daniel Moore', 'Priya Nair', 'Liam Wilson', 'Olivia Brooks', 'Marco Klein', 'Ava Johnson', 'Rafael Lima', 'Isabella Rossi', 'Hiro Tanaka', 'Fatima Khan'];
var managers = ['Olivia Brooks', 'Marco Klein', 'Sofia Lim', 'Priya Nair', 'Rafael Lima', 'Daniel Reed', 'Ava Johnson', 'Liam Wilson', 'Grace Bennett', 'Kunal Mehta', 'Noah Turner', 'Mila Carter'];
var channels = ['Online', 'Retail', 'Partner', 'Distributor', 'Direct'];
var remarkPool = ['Top seller in the region', 'Stable demand', 'Exceeded quota', 'High repeat orders', 'Important account', 'Low-risk replenishment', 'Need follow-up on renewal', 'Expansion opportunity', 'Seasonal spike observed', 'Pending contract signature', 'Bulk order secured', 'Watch for shipping delay'];
var round2 = (value) => Math.round(value * 100) / 100;
var pick = (arr) =>
arr[Math.floor(Math.random() * arr.length)];
var productVersionMap = {};
for (var index = 0; index < 100000; index++) {
var product = productCatalog[Math.floor(Math.random() * productCatalog.length)];
if (!productVersionMap[product.name]) {
productVersionMap[product.name] = 1;
}
var version = `V${productVersionMap[product.name]}`;
productVersionMap[product.name]++;
var salesQty = Math.floor(5 + Math.random() * 100);
var unitPrice = round2(
product.basePrice * (0.8 + Math.random() * 0.4)
);
var grossAmount = round2(salesQty * unitPrice);
var discount = round2(2 + Math.random() * 18);
var discountAmount = round2((grossAmount * discount) / 100);
var shippingCost = round2(25 + Math.random() * 150);
var tax = round2(5 + Math.random() * 15);
var netAmount = round2(
grossAmount - discountAmount + shippingCost * 0.15
);
var profit = round2(
grossAmount - discountAmount - shippingCost - grossAmount * (0.3 + Math.random() * 0.2)
);
var target = round2(
grossAmount * (1.2 + Math.random() * 1.5)
);
var forecast = round2(
target * (0.8 + Math.random() * 0.4)
);
var profitMargin =
grossAmount === 0 ? 0 : round2((profit / grossAmount) * 100);
var achievementPercent = round2(20 + Math.random() * 80);
var isZero = Math.random() < 0.05;
var returnQty = isZero
? 0
: Math.floor(1 + Math.random() * 5);
var returnAmount = returnQty > 0
? round2(returnQty * unitPrice * (0.5 + Math.random() * 0.3))
: 0;
salesDataSource.push({
ProductId: 100001 + index,
ProductName: `${product.name} ${version}`,
Region: product.region,
SalesQty: salesQty,
GrossAmount: grossAmount,
NetAmount: netAmount,
ProfitMargin: profitMargin,
AchievementPercent: achievementPercent,
UnitPrice: unitPrice,
Month: pick(months),
Category: product.category,
SubCategory: product.subCategory,
Brand: product.brand,
Country: product.country,
State: product.state,
City: product.city,
Discount: discount,
Tax: tax,
ShippingCost: shippingCost,
Profit: profit,
Target: target,
Forecast: forecast,
SalesRep: pick(salesReps),
Manager: pick(managers),
Channel: pick(channels),
Quarter: `Q${Math.floor(Math.random() * 4) + 1}`,
Year: 2023 + Math.floor(Math.random() * 3),
ReturnQty: returnQty,
ReturnAmount: returnAmount,
Remarks: pick(remarkPool)
});
}
}
function show() { document.getElementById('popup').style.display = 'inline-block'; }
function hide() { document.getElementById('popup').style.display = 'none'; }
// Syncfusion: the first click makes the 100,000 rows in the browser and binds the grid to them; the next ones read them again.
document.getElementById('generate').addEventListener('click', function () {
var grid = window.Lumen.get('InfiniteScroll');
show();
setTimeout(function () {
if (!salesDataSource.length) { createSalesDataSource(); }
grid.changeDataSource(salesDataSource);
}, 30);
});
hide();
</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/InfiniteScrolling.cshtml; it draws what ViewData holds.
public ActionResult InfiniteScrolling()
{
return View();
}
}
}
@* 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. *@
Infinite scrolling uses a lazy loading mechanism, where data is fetched automatically as the user scrolls to the end of the Grid. This behavior is enabled by setting the enableInfiniteScrolling property to true and defining the Grid’s height property.
The Grid also supports column virtualization, which renders only the visible columns to improve performance when working with a large number of columns. Column virtualization can be enabled by setting the enableColumnVirtualization property to true. Additionally, column virtualization integrates seamlessly with aggregate operations, ensuring that calculations like Sum, Average, etc., remain accurate and are displayed correctly during horizontal scrolling.