GridView.BestFitColumns() Method

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

Namespace: DevExpress.XtraGrid.Views.Grid

Assembly: DevExpress.XtraGrid.v20.1.dll

Declaration

public virtual void BestFitColumns()
Public Overridable Sub 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();
}
See Also