Edit this page

Resizing columns programmatically

Resizing columns

By default, the columns inside RadGridView are resizable. The user is able to easily change the column's width by positioning the mouse over the column's vertical grid line and dragging it until the desired size is achieved.

gridview-columns-rezise-columns001

Disabling resizing

There are two ways to disable resizing of columns in the user interface:

  • To restrict resizing of all columns by the user set the AllowColumnResize property of RadGridView to false

Disable resizing

this.radGridView1.AllowColumnResize = false;

Me.RadGridView1.AllowColumnResize = False

  • To restrict resizing of a particular column set the AllowResize property of the column to false.

Disable resizing for a specified column

this.radGridView1.Columns["Photo"].AllowResize = false;

Me.RadGridView1.Columns("Photo").AllowResize = False

Programmatically resizing columns

The width of columns can be set individually, per column. Note that the visible width will always include some amount of data even when set to very small amounts. To resize the columns programmatically you can use the Width property. For example:

Resize a column programmatically

this.radGridView1.Columns["Photo"].Width = 100;

Me.RadGridView1.Columns("Photo").Width = 100

You can restrict the possible values of the Width property by setting the MinWidth and MaxWidth properties. The MinWidth property sets a limit on how narrow the column can be resized. The MaxWidth property determines the limit on how the column width can grow.

Column Auto-Sizing

There are two ways to auto size the columns:

AutoSizeColumnsMode

Columns can automatically fill the entire width of the grid. Just set the AutoSizeColumnsMode property of the desired template to GridViewAutoSizeColumnsMode.Fill.

Best fit

The column widths can be set to fit its content by using the GridViewTemplate.BestFitColumns or GridViewDataColumn.BestFit methods. This mode distributes algorithm that attempts to fit in the header text and column data for all visible rows.

The user can perform the best fit operation:

  • By using the context menu of the header cell

  • By double clicking of the left or right header edge

You can customize the execution of the algorithm of a concrete column by setting the AutoSizeMode property on the desired column. Here are the available options:

  • BestFitColumnMode.None - The column's width does not automatically adjust.

  • BestFitColumnMode.AllCells - The column's width adjusts to fit the contents ofall cells in the control.

  • BestFitColumnMode.DisplayedDataCells - The column's width adjusts to fit the contents of the displayed data cells.

  • BestFitColumnMode.HeaderCells - The column's width adjusts to fit the contents of the header cell.

  • BestFitColumnMode.FilterCells - The column's width adjusts to fit the contents of the filter cell.

  • BestFitColumnMode.SummaryRowCells - The column's width adjusts to fit the contents of the summary row cell.

  • BestFitColumnMode.SystemCells - The column's width adjusts to fit the contents of the system cells.

  • BestFitColumnMode.DisplayedCells - The column's width adjusts to fit the contents of the displayed cells.

Resizing events

When the user resizes the column, the ColumnWidthChanging event is fired and if it is not canceled, the ColumnWidthChanged event fires after the resize operation is completed.

See Also