Columns in ASP.NET MVC Grid Component
26 Aug 202416 minutes to read
The column definitions are used as the DataSource
schema in the Grid. This plays a vital role in rendering column values in the required format.
The grid operations such as sorting, filtering and grouping etc. are performed based on column definitions. The Field
property of the Columns
is necessary to map the data source values in Grid columns.
NOTE
Column types
Column type can be specified using the Type
property of Columns
. It specifies the type of data the column binds.
If the Format
is defined for a column, the column uses Type
to select the appropriate format option number or date.
Grid column supports the following types:
- string
- number
- boolean
- date
- datetime
NOTE
If the
Type
is not defined, it will be determined from the first record of theDataSource
.
Incase if the first record of theDataSource
is null/blank value for a column then it is necessary to define theType
for that column.
Format
To format cell values based on specific culture, use the Format
property of Columns
. The grid uses Internalization library to format number and date values.
@Html.EJS().Grid("Format").DataSource((IEnumerable<object>)ViewBag.DataSource).Columns(col =>
{
col.Field("OrderID").HeaderText("Order ID").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Width("120").Add();
col.Field("CustomerID").HeaderText("Customer Name").Width("150").Add();
col.Field("OrderDate").HeaderText("Order Date").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Width("130").Format("yMd").Add();
col.Field("Freight").HeaderText("Freight").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Width("120").Format("C2").Add();
}).AllowPaging().Render()
public IActionResult Index()
{
var Order = OrderDetails.GetAllRecords();
ViewBag.DataSource = Order;
return View();
}
NOTE
By default, the number and date values are formatted in en-US locale.
Number formatting
The number or integer values can be formatted using the below format strings.
Format | Description | Remarks |
---|---|---|
N | Denotes numeric type. | The numeric format is followed by integer value as N2, N3. etc which denotes the number of precision to be allowed. |
C | Denotes currency type. | The currency format is followed by integer value as C2, C3. etc which denotes the number of precision to be allowed. |
P | Denotes percentage type | The percentage format expects the input value to be in the range of 0 to 1. For example the cell value 0.2 is formatted as 20%. The percentage format is followed by integer value as P2, P3. etc which denotes the number of precision to be allowed. |
Date formatting
You can format date values either using built-in date format string or custom format string.
For built-in date format you can specify Format
property as string(Example: yMd
).
You can also use custom format string to format the date values. Some of the custom formats and the formatted date values are given in the below table.
Format | Formatted value |
---|---|
{ type:’date’, format:’dd/MM/yyyy’ } | 04/07/1996 |
{ type:’date’, format:’dd.MM.yyyy’ } | 04.07.1996 |
{ type:’date’, skeleton:’short’ } | 7/4/96 |
{ type: ‘dateTime’, format: ‘dd/MM/yyyy hh:mm a’ } | 04/07/1996 12:00 AM |
{ type: ‘dateTime’, format: ‘MM/dd/yyyy hh:mm:ss a’ } | 07/04/1996 12:00:00 AM |
@Html.EJS().Grid("Format").DataSource((IEnumerable<object>)ViewBag.DataSource).Columns(col =>
{
col.Field("OrderID").HeaderText("Order ID").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Width("120").Add();
col.Field("CustomerID").HeaderText("Customer Name").Width("150").Add();
col.Field("OrderDate").HeaderText("Order Date").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Width("130").Format("yMd").Add();
col.Field("Freight").HeaderText("Freight").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Width("120").Format("C2").Add();
col.Field("Price").HeaderText("Price").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Width("120").Format("P2").Add();
}).AllowPaging().Render()
public IActionResult Index()
{
var Order = OrderDetails.GetAllRecords();
ViewBag.DataSource = Order;
return View();
}
Render boolean value as checkbox
To render boolean values as checkbox in columns, you need to set DisplayAsCheckBox
property as true.
@Html.EJS().Grid("Grid").DataSource((IEnumerable<object>)ViewBag.dataSource).Columns(col =>
{
col.Field("OrderID").HeaderText("Order ID").Width("120").ValidationRules(new { required = "true" }).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
col.Field("CustomerID").HeaderText("Customer Name").Width("150").ValidationRules(new { required = "true", minLength = 3 }).Add();
col.Field("Freight").HeaderText("Freight").Width("120").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
col.Field("ShipName").HeaderText("Ship Name").Width("150").Add();
col.Field("Verified").HeaderText("Verified").DisplayAsCheckBox(true).Width("150").Add();
}).AllowPaging().Render()
public IActionResult Index()
{
var orders = OrdersDetails.GetAllRecords();
ViewBag.DataSource = orders;
return View();
}
Visibility
You can hide any particular column in Grid before rendering by defining Visible
property as false. In the below sample ShipCity column is defined as visible false.
@Html.EJS().Grid("Visibility").DataSource((IEnumerable<object>)ViewBag.DataSource).Columns(col =>
{
col.Field("OrderID").HeaderText("Order ID").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
col.Field("CustomerID").HeaderText("Customer Name").Width("150").Add();
col.Field("ShippedDate").HeaderText("Shipped Date").Width("140").Format("yMd").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
col.Field("ShipCity").Visible(false).HeaderText("Ship City").Width("150").Add();
}).AllowPaging().Render()
public IActionResult Index()
{
var Order = OrderDetails.GetAllRecords();
ViewBag.DataSource = Order;
return View();
}
Lock columns
You can lock columns by using LockColumn
property. The locked columns will be moved to the first position. Also you cannot reorder its position.
In the below example, Ship City column is locked and its reordering functionality is disabled.
@Html.EJS().Grid("Reorder").DataSource((IEnumerable<object>)ViewBag.dataSource).AllowReordering().Columns(col =>
{
col.Field("OrderID").HeaderText("Order ID").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
col.Field("CustomerID").HeaderText("Customer ID").Width("150").Add();
col.Field("ShipCity").Width("130").LockColumn(true).Add();
col.Field("ShipName").Width("150").Add();
col.Field("ShipPostalCode").Width("125").Add();
col.Field("ShipRegion").Width("120").Add();
}).Render()
public IActionResult Index()
{
var order = OrderDetails.GetAllRecords();
ViewBag.DataSource = order;
return View();
}
Controlling Grid actions
You can enable or disable grid action for a particular column by setting the AllowFiltering
, AllowGrouping
, AllowEditing
,AllowReordering
, and AllowSorting
properties.
@Html.EJS().Grid("ControlGridAction").DataSource((IEnumerable<object>)ViewBag.DataSource).Columns(col =>
{
col.Field("OrderID").HeaderText("Order ID").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).AllowGrouping(false).Width("120").Add();
col.Field("CustomerID").HeaderText("Customer Name").Width("150").AllowFiltering(false).AllowEditing(false).AllowReordering(false).Add();
col.Field("OrderDate").HeaderText("Order Date").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Width("130").Format("yMd").Add();
col.Field("Freight").HeaderText("Freight").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Width("120").AllowSorting(false).Format("C2").Add();
}).AllowPaging().AllowGrouping().AllowFiltering().AllowSorting().AllowReordering().EditSettings(edit => { edit.AllowAdding(true).AllowEditing(true).AllowDeleting(true); }).Render()
public IActionResult Index()
{
var Order = OrderDetails.GetAllRecords();
ViewBag.DataSource = Order;
return View();
}
Show or hide columns by external button
You can show or hide grid columns dynamically using external buttons by invoking the showColumns or hideColumns method.
@Html.EJS().Button("Show").Content("Show").IsPrimary(true).Render()
@Html.EJS().Button("Hide").Content("Hide").IsPrimary(true).Render()
@Html.EJS().Grid("ShowHide").DataSource((IEnumerable<object>)ViewBag.DataSource).Columns(col =>
{
col.Field("OrderID").HeaderText("Order ID").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Width("120").Add();
col.Field("CustomerID").HeaderText("Customer Name").Width("150").Add();
col.Field("OrderDate").HeaderText("Order Date").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Width("130").Format("yMd").Add();
col.Field("Freight").HeaderText("Freight").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Width("120").Format("C2").Add();
}).AllowPaging().Render()
<script>
document.getElementById("Show").addEventListener("click", () => {
var grid = document.getElementById("ShowHide").ej2_instances[0];
grid.showColumns(['Customer Name', 'Order Date']); //show by HeaderText
});
document.getElementById("Hide").addEventListener("click", () => {
var grid = document.getElementById("ShowHide").ej2_instances[0];
grid.hideColumns(['Customer Name', 'Order Date']); //hide by HeaderText
})
</script>
public IActionResult Index()
{
var Order = OrderDetails.GetAllRecords();
ViewBag.DataSource = Order;
return View();
}
Customize column styles
You can customize the appearance of the header and content of a particular column using the customAttributes
property.
To customize the grid column, follow the given steps:
Step 1: Create a CSS class with custom style to override the default style for rowcell and headercell.
.e-grid .e-rowcell.customcss{
background-color: #ecedee;
color: 'red';
font-family: 'Bell MT';
font-size: 20px;
}
.e-grid .e-headercell.customcss{
background-color: #2382c3;
color: white;
font-family: 'Bell MT';
font-size: 20px;
}
Step 2: Add the custom CSS class to the specified column by using the customAttributes
property.
col.Field("Freight").HeaderText("Freight").Width("120").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).CustomAttributes(new { @class = "customcss" }).Add();
@Html.EJS().Grid("Grid").DataSource((IEnumerable<object>)ViewBag.DataSource).Columns(col =>
{
col.Field("OrderID").HeaderText("Order ID").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
col.Field("CustomerID").HeaderText("Customer Name").Width("150").Add();
col.Field("OrderDate").HeaderText("Order Date").Width("130").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Format("yMd").Add();
col.Field("Freight").HeaderText("Freight").Width("120").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).CustomAttributes(new { @class = "customcss" }).Add();
col.Field("ShipCountry").HeaderText("Ship Country").Width("120").Add();
}).AllowPaging().Render()
<style>
.e-grid .e-rowcell.customcss {
background-color: #ecedee;
color: red;
font-family: 'Bell MT';
font-size: 20px;
}
.e-grid .e-headercell.customcss {
background-color: #2382c3;
color: white;
font-family: 'Bell MT';
font-size: 20px;
}
</style>
public IActionResult Index()
{
ViewBag.DataSource = OrderDetails.GetAllRecords();
return View();
}
Display custom tooltip for columns
To display a custom ToolTip (EJ2 Tooltip), you can render the Grid control inside the Tooltip component and set the target as “.e-rowcell”. The tooltip is displayed when hovering the grid cells.
Change the tooltip content for the grid cells by using the following code in the (beforeRender) event.
function beforeRender(args) {
// event triggered before render the tooltip on target element.
var tooltip = document.getElementById("Tooltip").ej2_instances[0]
tooltip.content = args.target.closest("td").innerText;
}
@Html.EJS().Tooltip("Tooltip").Target(".e-rowcell").ContentTemplate(@<div>
@Html.EJS().Grid("Grid").DataSource((IEnumerable<object>)ViewBag.DataSource).Columns(col =>
{
col.Field("OrderID").HeaderText("Order ID").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
col.Field("CustomerID").HeaderText("Customer Name").Width("150").Add();
col.Field("OrderDate").HeaderText("Order Date").Width("130").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Format("yMd").Add();
col.Field("Freight").HeaderText("Freight").Width("120").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
col.Field("ShipCountry").HeaderText("Ship Country").Width("120").Add();
}).Render()
</div>).BeforeRender("beforeRender").Render()
<script>
function beforeRender(args) {
// event triggered before render the tooltip on target element.
var tooltip = document.getElementById("Tooltip").ej2_instances[0]
tooltip.content = args.target.closest("td").innerText;
}
</script>
public IActionResult Index()
{
ViewBag.DataSource = OrderDetails.GetAllRecords();
return View();
}
Align the text of Grid content and header
For aligning the text of Grid content and header part, use TextAlign and HeaderTextAlign properties.
Grid column supports the following alignments:
- Left
- Right
- Center
- Justify
@Html.EJS().Grid("Grid").DataSource((IEnumerable<object>)ViewBag.DataSource).Columns(col =>
{
col.Field("OrderID").HeaderText("Order ID").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).HeaderTextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
col.Field("CustomerID").HeaderText("Customer Name").Width("150").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Left).HeaderTextAlign(Syncfusion.EJ2.Grids.TextAlign.Left).Add();
col.Field("OrderDate").HeaderText("Order Date").Width("130").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Center).HeaderTextAlign(Syncfusion.EJ2.Grids.TextAlign.Center).Format("yMd").Add();
col.Field("Freight").HeaderText("Freight").Width("120").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Justify).HeaderTextAlign(Syncfusion.EJ2.Grids.TextAlign.Justify).Add();
}).Render()
public IActionResult Index()
{
var Order = OrderDetails.GetAllRecords();
ViewBag.DataSource = Order;
return View();
}
How to prevent checkbox in the blank row
By default, cells in the grid will be blank if the corresponding column values in the data source are null or undefined. The grid also has the option to prevent the rendering of checkboxes in such cases, even if the DisplayAsCheckBox property is set to true for that column, by using the RowDataBound event of the Grid.
In the following sample, the RowDataBound
event of the Grid is used to set the innerHTML of the checkbox element to empty.
@Html.EJS().Grid("Grid").DataSource((IEnumerable<object>)ViewBag.DataSource).Columns(col =>
{
col.Field("OrderID").HeaderText("Order ID").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
col.Field("CustomerID").HeaderText("Customer Name").Width("150").Add();
col.Field("OrderDate").HeaderText("Order Date").Width("130").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Format("yMd").Add();
col.Field("Verified").HeaderText("Verified").Width("120").DisplayAsCheckBox(true).Add();
}).RowDataBound("rowBound").Render()
<script>
function rowBound(args) {
let grid = document.getElementById('Grid').ej2_instances[0];
let count = 0;
let keys = Object.keys(args.data);
for (let i = 0; i < keys.length; i++) {
if (args.data[keys[i]] == null || args.data[keys[i]] == '' || args.data[keys[i]] == undefined) {
count++;
}
}
if (count == keys.length) {
for (let i = 0; i < grid.columns.length; i++) {
if (grid.columns[i].displayAsCheckBox) {
args.row.children[i].innerHTML = '';
}
}
}
}
</script>
public IActionResult Index()
{
var orders = OrdersDetails.GetAllRecords();
ViewBag.DataSource = orders;
return View();
}
See Also
- Group Column by Format
- How to set complex column as Foreignkey column
- Complex Data Binding with list of Array Of Objects
- How to add primaryKey after column rendered in ASP.NET MVC Grid
- Drag and drop between two Grids in ASP.NET MVC Grid
- How can I put a Sparkline in a child grid that is on a Grid with hierarchy in ASP.NET MVC Grid
- How can I enable or disable a menu option that is inside a template in ASP.NET MVC Grid
- How to change the data source or columns dynamically