Example of Column Options in ASP.NET Core Data Grid Control
Widths, formats, types, visibility and the other UiGridColumn options.
Every UiGridColumn setting in one grid. Scroll sideways: "Order #" stays on the left, "Shipped" on the right.
@Html.AntiForgeryToken()
@Html.UiGrid(new UiGridOptions
{
Id = "columnsGrid",
DataUrl = Url.Action("Orders", "GridDemo"),
AllowFiltering = true,
FilterType = UiGridFilterType.Excel,
AllowMultiSorting = true,
PageSize = 5,
Layout = UiGridLayout.FitData,
Columns = new List<UiGridColumn>
{
// Field: property name in the row (required). Visible = false hides the column.
new UiGridColumn { Field = "Id", Visible = false },
// HeaderText: custom caption. Freeze = Left: stays fixed while scrolling sideways.
// Width: fixed width in pixels.
new UiGridColumn
{
Field = "OrderNo",
HeaderText = "Order #",
Width = 130,
Freeze = UiFreezeDirection.Left
},
// FilterOperator: operator used by default in the filter popups (text columns start with "Starts With").
// FilterType: this column uses the Menu filter instead of the grid's Excel filter.
// MinWidth / MaxWidth: size limits in pixels.
new UiGridColumn
{
Field = "Customer",
HeaderText = "Customer Name",
MinWidth = 200,
MaxWidth = 320,
FilterType = UiGridFilterType.Menu,
FilterOperator = UiFilterOperator.Contains
},
// Type = Date + Format: how the date is shown (yyyy yy MM M dd d HH H hh h mm ss tt).
// TextAlign: cell alignment. HeaderTextAlign: header alignment (defaults to TextAlign).
new UiGridColumn
{
Field = "OrderDate",
HeaderText = "Order Date",
Type = UiColumnType.Date,
Format = "dd/MM/yyyy",
Width = 160,
TextAlign = UiTextAlign.Center,
HeaderTextAlign = UiTextAlign.Center
},
// Type = Number + Format: "C2" currency, "N2" 1,234.50, "N0" 1,235, "P1" percent.
// FilterType = CheckBox: only the checkbox list (no sort/custom-filter items).
new UiGridColumn
{
Field = "Amount",
HeaderText = "Amount (currency)",
Type = UiColumnType.Number,
Format = "C2",
Width = 190,
FilterType = UiGridFilterType.CheckBox
},
// Type = Boolean: shows a check mark; centred by default. Freeze = Right: fixed on the right.
// AllowSorting / AllowFiltering = false turn them off for this column only.
new UiGridColumn
{
Field = "Shipped",
HeaderText = "Shipped (no sort, no filter)",
Type = UiColumnType.Boolean,
Width = 230,
Freeze = UiFreezeDirection.Right,
AllowSorting = false,
AllowFiltering = false
}
}
})
@Html.AntiForgeryToken()
<p>Every UiGridColumn setting in one grid. Scroll sideways: "Order #" stays on the left, "Shipped" on the right.</p>
@Html.UiGrid(new UiGridOptions
{
Id = "columnsGrid",
DataUrl = Url.Action("Orders", "GridDemo"),
AllowFiltering = true,
FilterType = UiGridFilterType.Excel,
AllowMultiSorting = true,
PageSize = 5,
Layout = UiGridLayout.FitData,
Columns = new List<UiGridColumn>
{
// Field: property name in the row (required). Visible = false hides the column.
new UiGridColumn { Field = "Id", Visible = false },
// HeaderText: custom caption. Freeze = Left: stays fixed while scrolling sideways.
// Width: fixed width in pixels.
new UiGridColumn
{
Field = "OrderNo",
HeaderText = "Order #",
Width = 130,
Freeze = UiFreezeDirection.Left
},
// FilterOperator: operator used by default in the filter popups (text columns start with "Starts With").
// FilterType: this column uses the Menu filter instead of the grid's Excel filter.
// MinWidth / MaxWidth: size limits in pixels.
new UiGridColumn
{
Field = "Customer",
HeaderText = "Customer Name",
MinWidth = 200,
MaxWidth = 320,
FilterType = UiGridFilterType.Menu,
FilterOperator = UiFilterOperator.Contains
},
// Type = Date + Format: how the date is shown (yyyy yy MM M dd d HH H hh h mm ss tt).
// TextAlign: cell alignment. HeaderTextAlign: header alignment (defaults to TextAlign).
new UiGridColumn
{
Field = "OrderDate",
HeaderText = "Order Date",
Type = UiColumnType.Date,
Format = "dd/MM/yyyy",
Width = 160,
TextAlign = UiTextAlign.Center,
HeaderTextAlign = UiTextAlign.Center
},
// Type = Number + Format: "C2" currency, "N2" 1,234.50, "N0" 1,235, "P1" percent.
// FilterType = CheckBox: only the checkbox list (no sort/custom-filter items).
new UiGridColumn
{
Field = "Amount",
HeaderText = "Amount (currency)",
Type = UiColumnType.Number,
Format = "C2",
Width = 190,
FilterType = UiGridFilterType.CheckBox
},
// Type = Boolean: shows a check mark; centred by default. Freeze = Right: fixed on the right.
// AllowSorting / AllowFiltering = false turn them off for this column only.
new UiGridColumn
{
Field = "Shipped",
HeaderText = "Shipped (no sort, no filter)",
Type = UiColumnType.Boolean,
Width = 230,
Freeze = UiFreezeDirection.Right,
AllowSorting = false,
AllowFiltering = false
}
}
})
<p style="margin-top:8px">
<button type="button" class="btn btn-default btn-sm" onclick="Lumen.get('columnsGrid').setHeaderText('Customer', 'Client')">Rename "Customer Name" to "Client"</button>
<button type="button" class="btn btn-default btn-sm" onclick="Lumen.get('columnsGrid').freezeColumn('Customer', 'left')">Freeze "Customer Name"</button>
<button type="button" class="btn btn-default btn-sm" onclick="Lumen.get('columnsGrid').freezeColumn('Customer', 'none')">Unfreeze "Customer Name"</button>
</p>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
using Lumen.UI.Mvc.Demo.Models;
using Newtonsoft.Json;
// ServerOperations = true (default): the grid POSTs a UiDataRequest and expects { result, count } back (UiDataOperations.Execute).
// ServerOperations = false: return all rows once as a plain JSON array.
namespace Lumen.UI.Mvc.Demo.Controllers
{
public partial class GridDemoController : Controller
{
// SampleOrders.All is a shared in-memory list (also read by the chart demo); a real app would use its database instead.
private static readonly object Sync = SampleOrders.Sync;
[HttpPost]
[ValidateJsonAntiForgeryToken]
public ActionResult Orders(UiDataRequest request)
{
lock (Sync)
{
return Json(UiDataOperations.Execute(SampleOrders.All.AsQueryable(), request, "Id"));
}
}
}
}
namespace Lumen.UI.Mvc.Demo.Models
{
public class Order
{
public int Id { get; set; }
public string OrderNo { get; set; }
public DateTime OrderDate { get; set; }
public string Customer { get; set; }
/// <summary>Key of the customer (see SampleOrders.Customers), to show a foreign key column.</summary>
public int CustomerId { get; set; }
/// <summary>North, South, East or West: a column with few values, to show grouping and a dropdown editor.</summary>
public string Region { get; set; }
public decimal Amount { get; set; }
public bool Shipped { get; set; }
/// <summary>Long free text, to show text wrapping in the grid.</summary>
public string Notes { get; set; }
}
}
namespace Lumen.UI.Mvc.Demo.Models
{
/// <summary>In-memory test data for the grid and chart demos. A real app would query a database instead.</summary>
public static class SampleOrders
{
/// <summary>Lock held while reading or changing <see cref="All"/> (the grid demo edits it).</summary>
public static readonly object Sync = new object();
private static readonly string[] Customers =
{
"Santos Trading", "Reyes Hardware", "Dela Cruz Foods", "Bautista Auto Parts",
"Garcia Textiles", "Mendoza Electronics", "Torres Logistics", "Flores Bakery"
};
// Long notes to show text wrapping; some contain one very long word (a part number without spaces),
// which is broken inside the word only because it is wider than the column.
private static readonly string[] Notes =
{
"Deliver to the back entrance before 10 AM; the front gate is closed on weekends and public holidays.",
"Customer asked for partial delivery: send the available items now and the rest when back in stock.",
"Replacement part PN-XJ9000-ULTRAHEAVYDUTY-STAINLESSSTEELFLANGEASSEMBLY-REV7 must be checked before shipping.",
"Fragile.",
"Invoice must show the purchase order number, the project code and the name of the receiving warehouse.",
"Supercalifragilisticexpialidociousnessmaximizationprogramme discount applied at checkout.",
"Call the site supervisor thirty minutes before arrival so the loading bay can be cleared.",
"Standard delivery."
};
/// <summary>The values of Order.Region: the grouping demo's groups and the dropdown editor's choices.</summary>
public static readonly string[] Regions = { "North", "South", "East", "West" };
public static readonly List<Order> All = Build();
private static List<Order> Build()
{
// The demo builds made-up rows here (the fields are in the class above); a real app queries its database.
throw new NotImplementedException("Return your own rows.");
}
}
}
@* 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. *@