Example of Column Menu in ASP.NET Core Data Grid Control
This sample demonstrates the default functionalities of the ColumnMenu. Click the icon of each column to open the column menu.
Properties
@Html.UiGrid(new UiGridOptions
{
Id = "ColumnMenu",
Data = ViewData["dataSource"],
ShowColumnMenu = true,
OnColumnMenuClick = "columnMenuClick",
SelectionSettings = new UiSelectionSettings { AllowColumnSelection = true },
AllowSorting = true,
AllowFiltering = true,
AllowGrouping = true,
PageCount = 2,
AllowPaging = true,
PageSize = 12,
FilterType = UiGridFilterType.CheckBox,
GroupSettings = new UiGroupSettings { ShowGroupedColumn = true },
EditSettings = new UiEditSettings { AllowAdding = true, AllowEditing = true, AllowDeleting = true },
Toolbar = new List<string> { "Add", "Edit", "Delete", "Update", "Cancel" },
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "OrderID", HeaderText = "Order ID", Type = UiColumnType.Number, Width = 120, TextAlign = UiTextAlign.Right, IsPrimaryKey = true, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "CustomerID", HeaderText = "Customer Name", Width = 150, ShowInColumnChooser = false, ValidationRules = new UiValidationRules { Required = true, MinLength = 5 } },
new UiGridColumn { Field = "Freight", HeaderText = "Freight", Type = UiColumnType.Number, Width = 120, TextAlign = UiTextAlign.Right, Format = "C2", EditType = UiEditType.NumericEdit, ValidationRules = new UiValidationRules { Required = true, Min = 0 } },
new UiGridColumn { Field = "ShipName", HeaderText = "Ship Name", Width = 150 },
new UiGridColumn { Field = "ShipCountry", HeaderText = "Ship Country", Width = 150, EditType = UiEditType.DropDownEdit },
new UiGridColumn { Field = "ShipCity", HeaderText = "Ship City", Width = 150 }
}
})
<style>
#typeddl {
min-width: 100px;
}
</style>
@Html.UiGrid(new UiGridOptions
{
Id = "ColumnMenu",
Data = ViewData["dataSource"],
ShowColumnMenu = true,
OnColumnMenuClick = "columnMenuClick",
SelectionSettings = new UiSelectionSettings { AllowColumnSelection = true },
AllowSorting = true,
AllowFiltering = true,
AllowGrouping = true,
PageCount = 2,
AllowPaging = true,
PageSize = 12,
FilterType = UiGridFilterType.CheckBox,
GroupSettings = new UiGroupSettings { ShowGroupedColumn = true },
EditSettings = new UiEditSettings { AllowAdding = true, AllowEditing = true, AllowDeleting = true },
Toolbar = new List<string> { "Add", "Edit", "Delete", "Update", "Cancel" },
Columns = new List<UiGridColumn>
{
new UiGridColumn { Field = "OrderID", HeaderText = "Order ID", Type = UiColumnType.Number, Width = 120, TextAlign = UiTextAlign.Right, IsPrimaryKey = true, ValidationRules = new UiValidationRules { Required = true } },
new UiGridColumn { Field = "CustomerID", HeaderText = "Customer Name", Width = 150, ShowInColumnChooser = false, ValidationRules = new UiValidationRules { Required = true, MinLength = 5 } },
new UiGridColumn { Field = "Freight", HeaderText = "Freight", Type = UiColumnType.Number, Width = 120, TextAlign = UiTextAlign.Right, Format = "C2", EditType = UiEditType.NumericEdit, ValidationRules = new UiValidationRules { Required = true, Min = 0 } },
new UiGridColumn { Field = "ShipName", HeaderText = "Ship Name", Width = 150 },
new UiGridColumn { Field = "ShipCountry", HeaderText = "Ship Country", Width = 150, EditType = UiEditType.DropDownEdit },
new UiGridColumn { Field = "ShipCity", HeaderText = "Ship City", Width = 150 }
}
})
<div class="demo-property-panel">
<h3>Properties</h3>
<div class="demo-property-row"><label for="ColumnMenuDD">Column Menu</label><select id="ColumnMenuDD">@foreach (var option in (string[])ViewData["ddData"]) { <option value="@option">@option</option> }</select></div>
</div>
<script>
document.getElementById('ColumnMenuDD').value = 'Default';
// Syncfusion: Custom sets grid.columnMenuItems (built-in names and two items of its own) and grid.columnMenuClick; Default clears them.
document.getElementById('ColumnMenuDD').addEventListener('change', function (e) {
window.Lumen.get('ColumnMenu').setSettings({
columnMenuItems: e.target.value === 'Custom'
? ['sortascending', 'sortdescending', 'group', 'ungroup', 'filter', { id: 'select_column', text: 'Select Column' }, { id: 'clear_column', text: 'Clear column selection' }]
: []
});
});
function columnMenuClick(args) {
var grid = window.Lumen.get('ColumnMenu');
if (args.item.id === 'select_column') { grid.selectColumn(args.column.index); }
else if (args.item.id === 'clear_column') { grid.clearColumnSelection(); }
}
</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/ColumnMenu.cshtml; it draws what ViewData holds.
public ActionResult ColumnMenu()
{
var order = OrdersDetails.GetAllRecords();
ViewData["datasource"] = order;
ViewData["ddData"] = new string[] { "Default", "Custom" };
return View();
}
/// <summary>One order: the rows of Syncfusion's Grid samples that use the orders.</summary>
public class OrdersDetails
{
public OrdersDetails()
{
}
public OrdersDetails(int OrderID, string CustomerId, int EmployeeId, double Freight, bool Verified, DateTime OrderDate, string ShipCity, string ShipName, string ShipCountry, DateTime ShippedDate, string ShipAddress)
{
this.OrderID = OrderID;
this.CustomerID = CustomerId;
this.EmployeeID = EmployeeId;
this.Freight = Freight;
this.ShipCity = ShipCity;
this.Verified = Verified;
this.OrderDate = OrderDate;
this.ShipName = ShipName;
this.ShipCountry = ShipCountry;
this.ShippedDate = ShippedDate;
this.ShipAddress = ShipAddress;
}
public int OrderID { get; set; }
public string CustomerID { get; set; }
public int EmployeeID { get; set; }
public double Freight { get; set; }
public string ShipCity { get; set; }
public bool Verified { get; set; }
public DateTime OrderDate { get; set; }
public string ShipName { get; set; }
public string ShipCountry { get; set; }
public DateTime ShippedDate { get; set; }
public string ShipAddress { get; set; }
public static List<OrdersDetails> GetAllRecords()
{
List<OrdersDetails> order = new List<OrdersDetails>();
int code = 10000;
for (int i = 1; i < 10; i++)
{
order.Add(new OrdersDetails(code + 1, "ALFKI", i + 0, 2.3 * i, false, new DateTime(1991, 05, 15, 10, 40, 00), "Berlin", "Simons bistro", "Denmark", new DateTime(1996, 7, 16, 09, 30, 00), "Kirchgasse 6"));
order.Add(new OrdersDetails(code + 2, "ANATR", i + 2, 3.3 * i, true, new DateTime(1990, 04, 04, 09, 30, 00), "Madrid", "Queen Cozinha", "Brazil", new DateTime(1996, 9, 11, 03, 45, 00), "Avda. Azteca 123"));
order.Add(new OrdersDetails(code + 3, "ANTON", i + 1, 4.3 * i, true, new DateTime(1957, 11, 30, 03, 45, 00), "Cholchester", "Frankenversand", "Germany", new DateTime(1996, 10, 7, 07, 50, 00), "Carrera 52 con Ave. Bolívar #65-98 Llano Largo"));
order.Add(new OrdersDetails(code + 4, "BLONP", i + 3, 5.3 * i, false, new DateTime(1930, 10, 22, 07, 30, 00), "Marseille", "Ernst Handel", "Austria", new DateTime(1996, 12, 30, 06, 12, 00), "Magazinweg 7"));
order.Add(new OrdersDetails(code + 5, "BOLID", i + 4, 6.3 * i, true, new DateTime(1953, 02, 18, 05, 50, 00), "Tsawassen", "Hanari Carnes", "Switzerland", new DateTime(1997, 12, 3, 12, 50, 00), "1029 - 12th Ave. S."));
code += 5;
}
return order;
}
}
}
}
@* 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 provides an option to display a column menu when the user clicks the icon on each column. This menu includes integrated features like sorting, grouping, filtering, column chooser, and auto-fitting. These features can be enabled by setting the showColumnMenu property to `true`. The default menu items include:
SortAscending: Sort the current column in ascending order.SortDescending: Sort the current column in descending order.Group: Group the current column.Ungroup: Ungroup the current column.AutoFit: Auto-fit current column.AutoFitAll: Auto-fit all columns.ColumnChooser: Toggle column visibility.Filter: Display the filter option as specified in thefilterSetting->type.
The default column menu items are enabled based on the corresponding feature settings. Additionally, the column menu allows users to include custom menu items with the default ones, or the replacement of the default items entirely with custom options.
In this demo, the Column Menu feature is initially enabled by setting showColumnMenu to `true`, with sorting, grouping, filtering, column chooser, and auto-fit options. Users also have the option to toggle custom column menu items through using the dropdown list in the property panel. In the custom menu, sorting, grouping, and filtering are enabled, and custom options like column selection and clear selection have been added.