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

GridView.BestFitColumns() Method

Adjusts column widths so that columns fit their content in the optimal way.

Namespace: DevExpress.XtraGrid.Views.Grid

Assembly: DevExpress.XtraGrid.v19.2.dll

Declaration

public virtual void BestFitColumns()

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.

If a column’s OptionsColumn.FixedWidth option is enabled, the BestFitColumns method retains the column size.

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();
}

The following code snippets (auto-collected from DevExpress Examples) contain references to the BestFitColumns() 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