AutoFit Columns in ASP.NET Core Grid Component

18 Mar 20246 minutes to read

The Grid has a feature that allows it to automatically adjust column widths based on the maximum content width of each column when you double-click on the resizer symbol located in a specific column header.  This feature ensures that all data in the grid rows is displayed without wrapping. To use this feature, you need to enable the resizer symbol in the column header by setting the allowResizing property to true in the grid.

Resizing a Column to fit its content using autoFit method

The autoFitColumns method resizes the column to fit the widest cell’s content without wrapping. You can autofit a specific column at initial rendering by invoking the autoFitColumns method in dataBound event.

<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" dataBound="dataBound" height="315">   
    <e-grid-columns>
        <e-grid-column field="OrderID" headerText="Order ID"  width="140"></e-grid-column>
        <e-grid-column field="CustomerID" headerText="Customer ID" type="string" width="130"></e-grid-column>
        <e-grid-column field="ShipName" headerText="Ship Name" width="120"></e-grid-column>
        <e-grid-column field="ShipCity" headerText="Ship City" width="120"></e-grid-column>
        <e-grid-column field="ShipAddress" headerText="Ship Address" width="150"></e-grid-column>
    </e-grid-columns>
</ejs-grid>

<script>
    function dataBound(args) {
        this.autoFitColumns(['ShipName', 'ShipAddress']);    
    }
</script>
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}

You can autofit all the columns by invoking the autoFitColumns method without specifying column names.

AutoFit columns with empty space

The Autofit feature is utilized to display columns in a grid based on the defined width specified in the columns declaration. If the total width of the columns is less than the width of the grid, this means that white space will be displayed in the grid instead of the columns auto-adjusting to fill the entire grid width.

You can enable this feature by setting the autoFit property set to true. This feature ensures that the column width is rendered only as defined in the Grid column definition.

<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" height="400" width="850" allowResizing="true" autoFit="true">   
    <e-grid-columns>
        <e-grid-column field="OrderID" headerText="Order ID" minWidth="100" width="150" maxWidth="200" textAlign="Right"></e-grid-column>
        <e-grid-column field="CustomerID" headerText="Customer ID" minWidth="8" width="150"></e-grid-column>
        <e-grid-column field="Freight" headerText="Freight" minWidth="8" width="120" format="C2" textAlign="Right"></e-grid-column>
        <e-grid-column field="ShipCity" headerText="Ship City" allowResizing ="false" width="150" textAlign="Right"></e-grid-column>
        <e-grid-column field="ShipCountry" headerText="Ship Country" minWidth="8" width="150"></e-grid-column>
    </e-grid-columns>
</ejs-grid>
public IActionResult Index()
{
    var Order = OrderDetails.GetAllRecords();
    ViewBag.DataSource = Order;
    return View();
}

If any one of the column width is undefined, then the particular column will automatically adjust to fill the entire width of the grid table, even if you have enabled the autoFit property of grid.

AutoFit columns with specific rows

To adjust the column widths of a specific range of rows based on their content, you can use the autoFitColumns method by simply passing the second and third parameters (optional) as the start and end index for the column you want to fit. You can autofit specific columns at initial rendering by invoking the autoFitColumns method in dataBound event.

This feature will calculate the appropriate width based on the maximum content width of the specified range of rows or the header text width. Subsequently, the maximum width of the content of the specified rows or header text will be applied to the entire column of the grid.

Here is an example of how to autofit columns with specific rows. The first parameter is an array containing the specific column field names, such as Inventor, Number of INPADOC patents and Main fields of invention is passed to apply the autofit functionality to these columns. After, the second parameter are start index is set to 1 and third parameter are end index is set to 3. When specifying these start and end index, the autofit operation is applied only to the range of rows from 1 to 3 for column width adjustment.

<ejs-grid id="Grid" dataSource="@ViewBag.DataSource" dataBound="dataBound" height="315" allowResizing="true">   
    <e-grid-columns>
        <e-grid-column field="Inventor" headerText="Inventor" width="140" clipMode="EllipsisWithTooltip"></e-grid-column>
        <e-grid-column field="NumberofPatentFamilies" headerText="Number of Patent Families" type="string" width="130"></e-grid-column>
        <e-grid-column field="Country" headerText="Country" width="120"></e-grid-column>
        <e-grid-column field="Number of INPADOC patents" headerText="Number of INPADOC patents" width="120"></e-grid-column>
        <e-grid-column field="Active" headerText="Active" width="150"></e-grid-column>
        <e-grid-column field="Mainfieldsofinvention" headerText="Main fields of invention" width="150"></e-grid-column>
    </e-grid-columns>
</ejs-grid>

<script>
    function dataBound(args) {
        this.autoFitColumns(['Inventor', 'Number of INPADOC patents', 'Mainfieldsofinvention'], 1, 3);    
    }
</script>
public IActionResult Index()
{
    var orders = InventorDetails.GetAllRecords();
    ViewBag.datasource = orders;            
    return View();
}