Search results

Frozen Rows and Columns in ASP.NET MVC Grid control

02 Aug 2021 / 2 minutes to read

Frozen rows and columns provides an option to make rows and columns always visible in the top and left side of the grid while scrolling.

In this demo, the FrozenColumns is set as 2 and the FrozenRows is set as 3. Hence, the left two columns and top three rows are frozen.

razor
freeze-row-column.cs
Copied to clipboard
@Html.EJS().Grid("FrozenGrid").DataSource((IEnumerable<object>)ViewBag.datasource).AllowSelection(false).AllowResizing().Height("410").FrozenRows(2).FrozenColumns(1).Columns(col =>
{
   col.Field("OrderID").HeaderText("Order ID").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
   col.Field("Freight").HeaderText("Freight").Width("120").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
   col.Field("OrderDate").HeaderText("Order Date").Width("130").Format("yMd").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
   col.Field("CustomerID").HeaderText("Customer Name").Width("200").Add();
   col.Field("ShipName").HeaderText("Ship Name").Width("150").Add();
   col.Field("ShippedDate").HeaderText("Shipped Date").Width("130").Format("yMd").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
   col.Field("ShipCity").HeaderText("Ship City").Width("200").Add();
   col.Field("ShipAddress").HeaderText("Ship Address").Width("150").Add();
   col.Field("ShipCountry").HeaderText("Ship Country").Width("150").Add();

}).Render()
Copied to clipboard
public IActionResult Index()
 {
    ViewBag.datasource = OrdersDetails.GetAllRecords();            
    return View();
 }

Frozen rows and columns should not be set outside the grid view port. Frozen Grid will support row and column virtualization feature, which helps to improve the Grid performance while loading a large dataset.

Limitations of Frozen Grid

The following features are not supported in frozen rows and columns:

  • Grouping
  • Row Template
  • Detail Template
  • Hierarchy Grid

Freeze particular columns

To freeze particular column in the grid, the IsFrozen property can be used.

In this demo, the columns with field name OrderID and CustomerID is frozen using the IsFrozen property.

razor
freeze-column.cs
Copied to clipboard
@Html.EJS().Grid("FrozenGrid").DataSource((IEnumerable<object>)ViewBag.datasource).AllowSelection(false).AllowResizing().Height("410").Columns(col =>
{
   col.Field("OrderID").HeaderText("Order ID").Width("120").IsFrozen(true).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
   col.Field("Freight").HeaderText("Freight").Width("120").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
   col.Field("OrderDate").HeaderText("Order Date").Width("130").Format("yMd").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
   col.Field("CustomerID").HeaderText("Customer Name").IsFrozen(true).Width("200").Add();
   col.Field("ShipName").HeaderText("Ship Name").Width("150").Add();
   col.Field("ShippedDate").HeaderText("Shipped Date").Width("130").Format("yMd").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
   col.Field("ShipCity").HeaderText("Ship City").Width("200").Add();
   col.Field("ShipAddress").HeaderText("Ship Address").Width("150").Add();
   col.Field("ShipCountry").HeaderText("Ship Country").Width("150").Add();

}).Render()
Copied to clipboard
public IActionResult Index()
 {
    var orders = OrderDetails.GetAllRecords();
    ViewBag.datasource = orders;            
    return View();
  }

Freeze Direction

You can freeze the Grid columns on the left or right side by using the column.freeze property and the remaining columns will be movable. The grid will automatically move the columns to the left or right position based on the column.freeze value.

Types of the column.freeze directions:

  • Left: Allows you to freeze the columns at the left.
  • Right: Allows you to freeze the columns at the right.

In this demo, the ShipCountry column is frozen at the left and the CustomerID column is frozen at the right side of the content table.

razor
freeze-column.cs
Copied to clipboard
@Html.EJS().Grid("FrozenGrid").DataSource((IEnumerable<object>)ViewBag.datasource).FrozenRows(2).Height("410").Columns(col =>
{
    col.Field("OrderID").HeaderText("Order ID").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("Freight").HeaderText("Freight").Width("120").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("CustomerID").HeaderText("Customer ID").Width("150").Freeze(Syncfusion.EJ2.Grids.FreezeDirection.Right).Add();
    col.Field("EmployeeID").HeaderText("Employee ID").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("OrderDate").HeaderText("Order Date").Width("130").Format("yMd").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("ShipName").HeaderText("Ship Name").Width("150").Add();
    col.Field("ShipAddress").HeaderText("Ship Address").Width("150").Add();
    col.Field("ShipCity").HeaderText("Ship City").Width("150").Add();
    col.Field("ShipCountry").HeaderText("Ship Country").Width("150").Freeze(Syncfusion.EJ2.Grids.FreezeDirection.Left).Add();
}).Render()
Copied to clipboard
public IActionResult Index()
 {
    var orders = OrderDetails.GetAllRecords();
    ViewBag.datasource = orders;            
    return View();
  }
  • Freeze Direction is not compatible with the isFrozen and frozenColumns properties.

Limitations of Freeze Direction

This feature has the below limitations, along with the above mentioned Frozen Grid limitations.

  • Column virtualization
  • Infinite scroll cache mode
  • Freeze direction in the stacked header is not compatible with column reordering.