Skip to main content

GridView.BestFitColumns(Boolean) Method

Adjusts column widths so that columns fit their content in an optimal way. The method’s parameter allows you to forcibly adjust column widths regardless of whether column resizing is prohibited.

Namespace: DevExpress.XtraGrid.Views.Grid

Assembly: DevExpress.XtraGrid.v24.2.dll

Declaration

public virtual void BestFitColumns(
    bool forceResize
)

Parameters

Name Type Description
forceResize Boolean

true to forcibly adjust column widths even if column resizing is prohibited; otherwise, false.

Remarks

Call the BestFitColumns method to resize columns to the minimum width required to accommodate the content of the following elements:

DevExpress WinForms Data Grid BestFitColumns method

Set the forceResize parameter to true to forcibly adjust column widths regardless of GridOptionsCustomization.AllowColumnResizing and OptionsColumn.AllowSize property values.

Specific Notes

  • If the GridOptionsView.ColumnAutoWidth property is enabled, the BestFitColumns method resizes columns to fit their content within the View’s width. Cell content may still be truncated.

  • The BestFitColumns method retains a column’s width if the column’s OptionsColumn.FixedWidth option is enabled.

Example

The following code snippet updates column widths when users change cell values:

private void GridView1_CellValueChanged(object sender, CellValueChangedEventArgs e) {
  gridView1.BestFitColumns();
}

DevExpress WinForms Data Grid apply BestFit on value change

API

Description

GridOptionsView.BestFitMaxRowCount

Gets or sets the number of data rows to process for optimal width calculation.

GridOptionsView.BestFitUseErrorInfo

Gets or sets whether cell error icons are taken into account when calculating the “best width” for columns.

GridOptionsView.BestFitMode

Gets or sets whether column best-fit resizing operations should prioritize precision or speed.

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the BestFitColumns(Boolean) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

See Also