Column Resizing

2 Mar 20224 minutes to read

Column width can be resized by clicking and dragging the right edge of the column header. While dragging, the width of the respective column will be resized immediately. Each column can be auto resized by double-clicking the right edge of the column header to fit the width of that column based on the widest cell content. To enable column resize, set the AllowResizing property to true.

@Html.EJS().Grid("ColumnResize").DataSource((IEnumerable<object>)ViewBag.dataSource).AllowResizing(true).Columns(col =>
{
   col.Field("OrderID").HeaderText("Order ID").Width("200").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
   col.Field("CustomerID").HeaderText("Customer Name").Width("150").Add();
   col.Field("OrderDate").HeaderText("Order Date").Width("200").Format("yMd").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
   col.Field("Freight").HeaderText("Freight").Width("150").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
   col.Field("ShipName").HeaderText("Ship Country").Width("300").Add();
   col.Field("ShippedDate").HeaderText("Shipped Date").Width("200").Format("yMd").AllowResizing(false).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
   col.Field("ShipName").HeaderText("Ship Country").Width("200").Add();
   col.Field("ShipName").HeaderText("Ship Country").Width("200").Add();

}).AllowPaging().Render()
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}

You can disable resizing for a particular column by setting the AllowResizing property of Columns to false.
In RTL mode, you can click and drag the left edge of the header cell to resize the column.

Column resizing using method

To resize a column, set width to that particular column and then refresh the grid header by using the refreshHeader method. Please refer the below code

var grid = document.getElementById('Grid').ej2_instances[0]; //Grid Instance

var columns = grid.columns;

columns[0].width = 150;

grid.refreshHeader();

Min and max width

Column resize can be restricted between minimum and maximum width by defining the MinWidth and MaxWidth properties of Columns.

In the following sample, minimum and maximum width are defined for OrderID, Ship Name, and Ship Country columns.

@Html.EJS().Grid("ColumnResizeMinMax").DataSource((IEnumerable<object>)ViewBag.dataSource).AllowResizing(true).Columns(col =>
{
  col.Field("OrderID").HeaderText("Order ID").MinWidth("120").Width("200").MaxWidth("300").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
  col.Field("CustomerID").HeaderText("Customer Name").Width("150").Add();
  col.Field("OrderDate").HeaderText("Order Date").Width("200").Format("yMd").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
  col.Field("Freight").HeaderText("Freight").Width("150").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
  col.Field("ShippedDate").HeaderText("Shipped Date").Width("200").Format("yMd").AllowResizing(false).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
  col.Field("ShipName").HeaderText("Ship Country").MinWidth("30").Width("100").MaxWidth("200").Add();
  col.Field("ShipCountry").HeaderText("Ship Country").MinWidth("50").Width("200").MaxWidth("300").Add();

}).AllowPaging().Render()
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}

Resize stacked column

Stacked columns can be resized by clicking and dragging the right edge of the stacked column header. While dragging, the width of the respective child columns will be resized at the same time. You can disable resize for any particular stacked column by setting AllowResizing as false to its columns.

In this example, we have disabled resize for Ship City column.

@Html.EJS().Grid("StackedColumnResize").DataSource((IEnumerable<object>)ViewBag.dataSource).AllowResizing(true).Columns(col =>
{
  col.Field("OrderID").HeaderText("Order ID").Width("100").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
  col.HeaderText("Order Details").Columns(new List<Syncfusion.EJ2.Grids.GridColumn>() { new Syncfusion.EJ2.Grids.GridColumn { Field = "OrderDate", Width = "120", HeaderText = "Order Date",TextAlign=Syncfusion.EJ2.Grids.TextAlign.Right,Format="yMd" },
        new Syncfusion.EJ2.Grids.GridColumn { Field = "Freight", Width = "100", TextAlign=Syncfusion.EJ2.Grids.TextAlign.Right, HeaderText = "Freight($)", Format="C2" } }).Add();
  col.HeaderText("Shipped Details").Columns(new List<Syncfusion.EJ2.Grids.GridColumn>() { new Syncfusion.EJ2.Grids.GridColumn { Field = "ShipCity", Width = "100", HeaderText = "Ship City", TextAlign=Syncfusion.EJ2.Grids.TextAlign.Right, AllowResizing = false },
        new Syncfusion.EJ2.Grids.GridColumn { Field = "ShipCountry", Width = "120",  HeaderText = "Ship Country" } }).Add();
}).AllowPaging().Render()
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}

Touch interaction

When the right edge of the header cell is tapped, a floating handler will be visible over the right border of the column. To resize the column, tap and drag the floating handler as needed. You can autoFit a column by using the Column menu of the grid.

The following screenshot represents the column resizing in touch device.

Touch interaction

Resizing events

During the resizing action, the grid component triggers the below three events.

  1. The ResizeStart event triggers when column resize starts.
  2. The Resizing event triggers when column header element is dragged (moved) continuously.
  3. The ResizeStop event triggers when column resize ends.
@Html.EJS().Grid("ColumnResize").DataSource((IEnumerable<object>)ViewBag.dataSource).AllowResizing(true).Columns(col =>
{
   col.Field("OrderID").HeaderText("Order ID").Width("200").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
   col.Field("CustomerID").HeaderText("Customer Name").Width("150").Add();
   col.Field("OrderDate").HeaderText("Order Date").Width("200").Format("yMd").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
   col.Field("Freight").HeaderText("Freight").Width("150").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
   col.Field("ShipName").HeaderText("Ship Country").Width("300").Add();
   col.Field("ShippedDate").HeaderText("Shipped Date").Width("200").Format("yMd").AllowResizing(false).TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
   col.Field("ShipName").HeaderText("Ship Country").Width("200").Add();

}).AllowPaging().ResizeStart("resizeStart").Resizing("resizing").ResizeStop("resizeStop").Render()

<script>

    function resizeStart() {
        alert('resizeStart event is triggered')
    }
    function resizing() {
        alert('resizing event is triggered')
    }
    function resizeStop() {
        alert('resizeStop event is triggered')
    }

</script>
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}