Skip to main content

GridColumn.BestFit() Method

Resizes the column to the minimum width required to display the column content completely.

Namespace: DevExpress.XtraGrid.Columns

Assembly: DevExpress.XtraGrid.v22.2.dll

NuGet Package: DevExpress.Win.Grid


public virtual void BestFit()


The BestFit method calculates the minimum width that is required to accommodate the content of the following elements:

Note that the number of processed data cells depends upon the GridOptionsView.BestFitMaxRowCount property value. If set to -1, all data cells are processed. If set to a positive integer, the property specifies the number of cells to be processed. Cells are processed starting from the top visible row.

When the auto width feature is applied to the view, the column element content may not be displayed completely after BestFit. The reason is that total column width always matches the View width - while columns have a minimum allowed width and may have a fixed width. The auto width feature can be enabled/disabled using the GridOptionsView.ColumnAutoWidth option. Refer to the GridColumn.MinWidth property and OptionsColumn.FixedWidth option for details on column width restrictions. Note that band width restrictions also affect the BestFit functionality. Refer to the OptionsBand.FixedWidth and GridBand.MinWidth members for details.

Also note that calculated column width cannot exceed the current View width. Finally, the BestFit method cannot resize columns to widths less than that specified by the GridColumn.MinWidth property.

Your users can also apply best fit to columns. They need to double-click the desired column’s right edge or choose the Best Fit item from the column header’s context menu. The column’s OptionsColumn.FixedWidth option must be disabled and the OptionsColumn.AllowSize option must be enabled to activate this functionality.

The BestFit method is not supported in CardView.

The code sample below illustrates how to change the column width when a user changes a cell value.

using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;


gridView1.CellValueChanged += GridView1_CellValueChanged1;

private void GridView1_CellValueChanged1(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
    GridView view = sender as GridView;

See Also