Example of DataTable Binding in ASP.NET Core Data Grid Control
This sample demonstrates the way of data binding Grid component with DataTable.
@* The grid's settings are in the view; the data they read (ViewData) is set in the controller action (see the controller tab). *@
@Html.UiGrid(new UiGridOptions
{
Id = "LocalData",
Data = ViewData["dataSource"],
AllowPaging = true,
PageSize = 12,
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "OrderID", HeaderText = "Order ID", TextAlign = UiTextAlign.Right, Width = 120 },
new UiGridColumn { Field = "EmployeeID", HeaderText = "Employee ID", TextAlign = UiTextAlign.Right, Width = 90 },
new UiGridColumn { Field = "CustomerID", HeaderText = "Customer Name", Width = 70 },
new UiGridColumn { Field = "Freight", HeaderText = "Freight", Type = UiColumnType.Number, Width = 90, TextAlign = UiTextAlign.Right, Format = "C2" },
new UiGridColumn { Field = "ShipCountry", HeaderText = "Ship Country", Width = 130 },
new UiGridColumn { Field = "OrderDate", HeaderText = "Order Date", Type = UiColumnType.Date, Width = 130, TextAlign = UiTextAlign.Right, Format = "M/d/yyyy" }
}
})
@Html.UiGrid(new UiGridOptions
{
Id = "LocalData",
Data = ViewData["dataSource"],
AllowPaging = true,
PageSize = 12,
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "OrderID", HeaderText = "Order ID", TextAlign = UiTextAlign.Right, Width = 120 },
new UiGridColumn { Field = "EmployeeID", HeaderText = "Employee ID", TextAlign = UiTextAlign.Right, Width = 90 },
new UiGridColumn { Field = "CustomerID", HeaderText = "Customer Name", Width = 70 },
new UiGridColumn { Field = "Freight", HeaderText = "Freight", Type = UiColumnType.Number, Width = 90, TextAlign = UiTextAlign.Right, Format = "C2" },
new UiGridColumn { Field = "ShipCountry", HeaderText = "Ship Country", Width = 130 },
new UiGridColumn { Field = "OrderDate", HeaderText = "Order Date", Type = UiColumnType.Date, Width = 130, TextAlign = UiTextAlign.Right, Format = "M/d/yyyy" }
}
})
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/DatatableBinding.cshtml; it draws what ViewData holds.
public ActionResult DatatableBinding()
{
DataTable dt = new DataTable("Table");
dt.Columns.AddRange(new DataColumn[6] { new DataColumn("OrderID", typeof(long)),
new DataColumn("EmployeeID",typeof(int)),
new DataColumn("CustomerID", typeof(string)),
new DataColumn("Freight", typeof(double)),
new DataColumn("OrderDate",typeof(DateTime)),
new DataColumn("ShipCountry",typeof(string))
});
int code = 10000;
for (int i = 1; i < 10; i++)
{
dt.Rows.Add(code + 1, i + 0, "ALFKI", 2.3 * i, new DateTime(1991, 05, 15), "Denmark");
dt.Rows.Add(code + 2, i + 2, "ANATR", 3.3 * i, new DateTime(1990, 04, 04), "Brazil");
dt.Rows.Add(code + 3, i + 1, "ANTON", 4.3 * i, new DateTime(1957, 11, 30), "Germany");
dt.Rows.Add(code + 4, i + 3, "BLONP", 5.3 * i, new DateTime(1930, 10, 22), "Austria");
dt.Rows.Add(code + 5, i + 4, "BOLID", 6.3 * i, new DateTime(1953, 02, 18), "Switzerland");
code += 5;
}
ViewData["datasource"] = dt;
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. *@
DataTable represents a table with relational data which has in-built schema to work easily with data column and data row objects.
You can bind DataTable to the grid by using DataSource property. While binding DataTable like above, grid actions such as Sorting, Filtering, Grouping, Paging etc., will handle at client side.