Column Resizing
24 Feb 20229 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.
<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" allowResizing="true" height="315">
<e-grid-columns>
<e-grid-column field="OrderID" headerText="Order ID" width="150" ></e-grid-column>
<e-grid-column field="CustomerID" headerText="Customer ID" width="150"></e-grid-column>
<e-grid-column field="Freight" headerText="Freight" width="150"></e-grid-column>
<e-grid-column field="ShipCity" headerText="Ship City" width="150"></e-grid-column>
<e-grid-column field="ShipName" headerText="Ship Name" width="150"></e-grid-column>
<e-grid-column field="ShipAddress" headerText="Ship Address" width="150"></e-grid-column>
<e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column>
<e-grid-column field="ShipPostalCode" headerText="Ship Postal Code width="150"></e-grid-column>
</e-grid-columns>
</ejs-grid>
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 e-grid-column tag helper 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 in e-grid-column tag helper.
In the following sample, minimum and maximum width are defined for OrderID, Ship Name, and Ship Country columns.
<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" allowResizing="true" height="315">
<e-grid-columns>
<e-grid-column field="OrderID" headerText="Order ID" minWidth="100" width="150" maxWidth="300"></e-grid-column>
<e-grid-column field="CustomerID" headerText="Customer ID" width="150"></e-grid-column>
<e-grid-column field="Freight" headerText="Freight" width="150"></e-grid-column>
<e-grid-column field="ShipCity" headerText="Ship City" width="150"></e-grid-column>
<e-grid-column field="ShipName" headerText="Ship Name" minWidth="120" width="150" maxWidth="250"></e-grid-column>
<e-grid-column field="ShipAddress" headerText="Ship Address" width="150"></e-grid-column>
<e-grid-column field="ShipCountry" headerText="Ship Country" minWidth="150" width="150" maxWidth="350"></e-grid-column>
<e-grid-column field="ShipPostalCode" headerText="Ship Postal Code" width="150"></e-grid-column>
</e-grid-columns>
</ejs-grid>
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
property e-grid-column as false to its columns.
In this example, we have disabled resize for Ship City column.
<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" allowResizing="true" height="315">
<e-grid-columns>
<e-grid-column field="OrderID" headerText="Order ID" isPrimaryKey="true" textAlign="Right" width="100"></e-grid-column>
<e-grid-column headerText="Order Details" columns="@( new List<Syncfusion.EJ2.Grids.GridColumn>() { new Syncfusion.EJ2.Grids.GridColumn { Field = "OrderDate", Width = "120", HeaderText = "Order Date", Format="yMd", TextAlign= Syncfusion.EJ2.Grids.TextAlign.Right }, new Syncfusion.EJ2.Grids.GridColumn { Field = "Freight", Width = "100", HeaderText = "Freight($)", Format="C2", TextAlign= Syncfusion.EJ2.Grids.TextAlign.Right } } )">
</e-grid-column>
<e-grid-column 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, Format="yMd" }, new Syncfusion.EJ2.Grids.GridColumn { Field = "ShipCountry", Width = "120", HeaderText = "Ship Country" } } )">
</e-grid-column>
</e-grid-columns>
</ejs-grid>
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.
Resizing events
During the resizing action, the grid component triggers the below three events.
- The
resizeStart
event triggers when column resize starts. - The
resizing
event triggers when column header element is dragged (moved) continuously. - The
resizeStop
event triggers when column resize ends.
<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" allowResizing="true" height="315" resizeStart="resizeStart"
resizing="resizing" resizeStop="resizeStop">
<e-grid-columns>
<e-grid-column field="OrderID" headerText="Order ID" width="150" ></e-grid-column>
<e-grid-column field="CustomerID" headerText="Customer ID" width="150"></e-grid-column>
<e-grid-column field="Freight" headerText="Freight" width="150"></e-grid-column>
<e-grid-column field="ShipCity" headerText="Ship City" width="150"></e-grid-column>
<e-grid-column field="ShipName" headerText="Ship Name" width="150"></e-grid-column>
<e-grid-column field="ShipAddress" headerText="Ship Address" width="150"></e-grid-column>
<e-grid-column field="ShipCountry" headerText="Ship Country" width="150"></e-grid-column>
<e-grid-column field="ShipPostalCode" headerText="Ship Postal Code" width="150"></e-grid-column>
</e-grid-columns>
</ejs-grid>
<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();
}