New to Telerik UI for Blazor? Download free 30-day trial

Resize Columns

The Grid features two different column resizing mechanisms:

The example at the end of this page shows both options in action.

Resize by Dragging

The Grid allows users to resize columns by dragging the borders between header cells.

To enable column resizing, set the Resizable parameter of the grid to true.

To prevent the user from resizing a certain column, set its own parameter Resizable="false". The user will still be able to resize other columns around it.

Here a few notes on the resizing behavior:

  • If the column Width is less than MinResizableWidth and the user tries to resize the column, it will snap to its minimum width.
  • Similarly, if the column Width is greater than MaxResizableWidth, the column will snap to its maximum width.
  • In multi-column header scenarios, you may set MinResizableWidth or MaxResizableWidth to child columns only. Setting these attributes to parent columns will have no effect.

Autofit Columns

When column resizing is enabled, a double click on the resize handle between the header cells will automatically fit the column width to the content of the header, data and footers. This will remove text wrapping in the component.

The Grid also exposes methods to programmatically resize columns to fit their contents:

  • AutoFitColumn(string id) - autofits the column with the specified Id attribute;
  • AutoFitColumns(IEnumerable<string> ids) - autofits multiple columns at once'
  • AutoFitAllColumns() - autofits all applicable columns (for example, this method does not affect the hierarchy expand/collapse columns);

Autofitting specific columns preserves the current widths of all the other columns. Similar to column resizing, column autofitting can trigger a horizontal Grid scrollbar, or leave empty space after the last column.

Programmatic autofitting works even if column resizing is disabled.

Limitations

List of the known limitations of the AutoFit Columns feature:

  • Auto-fitting the columns on initial load of the Grid is not supported.

  • Auto-fitting the columns is not supported with Virtual Columns

Check the AutoFit all Grid columns on initial load knowledge-based article to see a possible solution to achieve this behavior.

Example

How Column Resizing Works in the Telerik Grid

Grid Column Resizing and Autofitting

@* Grid column resizing and autofitting *@
@* Drag the border between column headers to change the column width. The command column is not resizable by the user. *@

<TelerikButton OnClick="@AutoFitSingleColumn">AutoFit ID Column</TelerikButton>
<TelerikButton OnClick="@AutoFitMultipleColumns">AutoFit String Columns</TelerikButton>
<TelerikButton OnClick="@AutoFitAllColumns">AutoFit All Columns</TelerikButton>

<TelerikGrid @ref="@Grid"
             Data="@GridData"
             Resizable="true"
             Pageable="true" PageSize="10" Sortable="true" Height="300px">
    <GridColumns>
        <GridColumn Field=@nameof(SampleData.Id) Title="ID" Id="IDColumn" />
        <GridColumn Field=@nameof(SampleData.Name) Title="First Name" Id="NameColumn1" />
        <GridColumn Field=@nameof(SampleData.LastName) Title="Last Name" Id="NameColumn2" />
        <GridCommandColumn Width="100px" Resizable="false">
            <GridCommandButton Command="Save" Icon="save" ShowInEdit="true">Update</GridCommandButton>
            <GridCommandButton Command="Edit" Icon="edit">Edit</GridCommandButton>
            <GridCommandButton Command="Delete" Icon="delete">Delete</GridCommandButton>
            <GridCommandButton Command="Cancel" Icon="cancel" ShowInEdit="true">Cancel</GridCommandButton>
        </GridCommandColumn>
    </GridColumns>
</TelerikGrid>

@code {
    public TelerikGrid<SampleData> Grid { get; set; }
    public List<SampleData> GridData { get; set; }

    private void AutoFitSingleColumn()
    {
        Grid.AutoFitColumn("IDColumn");
    }

    private void AutoFitMultipleColumns()
    {
        var columns = new List<string>() { "NameColumn1", "NameColumn2" };
        Grid.AutoFitColumns(columns);
    }

    private void AutoFitAllColumns()
    {
        Grid.AutoFitAllColumns();
    }

    protected override void OnInitialized()
    {
        GridData = GetData();
    }

    private List<SampleData> GetData()
    {
        return Enumerable.Range(1, 50).Select(x => new SampleData
        {
            Id = x,
            Name = $"name {x}",
            LastName = $"Surname {x}"
        }).ToList();
    }

    public class SampleData
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string LastName { get; set; }
    }
}

See Also

In this article
Not finding the help you need? Improve this article