Skip to main content
A newer version of this page is available. .

GridView.BestFitColumns(Boolean) Method

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

Namespace: DevExpress.XtraGrid.Views.Grid

Assembly: DevExpress.XtraGrid.v19.2.dll

Declaration

public virtual void BestFitColumns(
    bool forceResize
)

Parameters

Name Type Description
forceResize Boolean

true, if column widths should be forcibly adjusted even if column resizing is prohibited; otherwise, false.

Remarks

Call the BestFitColumns method to resize columns so that they completely display their content. Note that BestFitColumns resizes columns to the minimum width required. When it calculates the required column width, it evaluates the cell contents: column header contents, data cell contents and the footer summary cell’s content. If the View’s GridOptionsView.BestFitMaxRowCount property value is -1, BestFitColumns processes all data cells; otherwise, only the number of cells specified by that property value.

If the columns’ auto width feature (see the GridOptionsView.ColumnAutoWidth property) is in effect, the columns’ total width always matches the View’s width. In this case, the BestFitColumns method resizes columns proportionally with respect to their content.

Set the forceResize parameter to true to forcibly adjust the column widths, regardless of whether the GridOptionsCustomization.AllowColumnResizing and OptionsColumn.AllowSize properties prohibit the column size change.

The BestFitColumns method does not affect columns with fixed widths (see the the OptionsColumn.FixedWidth property).

To apply the best width feature to a particular column, call the column’s GridColumn.BestFit method.

Note

If you need to invoke the best fit functionality in the form’s System.Windows.Forms.Form.Load event handler, you should call the GridControl.ForceInitialize method in advance.

The code sample below illustrates how to update column widths when users change cell values.

gridView1.CellValueChanged += GridView1_CellValueChanged;

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

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