windowsforms-devexpress-dot-xtragrid-dot-views-dot-grid-dot-gridview-f389693c.md
Adjusts column widths so that columns fit their content in an optimal way.
Namespace : DevExpress.XtraGrid.Views.Grid
Assembly : DevExpress.XtraGrid.v25.2.dll
NuGet Packages : DevExpress.Win.Grid, DevExpress.Win.Navigation
public virtual void BestFitColumns()
Public Overridable Sub BestFitColumns
Call the BestFitColumns method to resize columns to the minimum width required to accommodate the content of the following elements:
Note
Call the GridControl.ForceInitialize method first to invoke the best fit functionality in the form’s Load event handler.
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 or the OptionsColumn.AllowSize property is disabled.
A BestFitColumns method call has no effect if the GridOptionsCustomization.AllowColumnResizing option is disabled.
The following code snippet updates column widths when users change cell values:
private void GridView1_CellValueChanged(object sender, CellValueChangedEventArgs e) {
gridView1.BestFitColumns();
}
Private Sub GridView1_CellValueChanged(ByVal sender As Object, ByVal e As CellValueChangedEventArgs)
gridView1.BestFitColumns()
End Sub
|
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.
| |
|
Gets or sets whether column best-fit resizing operations should prioritize precision or speed.
|
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.
scheduledJobsGrid.DataSource = scheduledJobs;
scheduledJobsView.BestFitColumns();
}
winforms-grid-bind-to-azure-sql-database/CS/DXApplication1/Main.cs#L14
gridControl.DataSource = await DataTableProvider.Instance.GetDataAsync();
gridView.BestFitColumns();
gridView.LoadingPanelVisible = false;
winforms-grid-data-annotation-attributes/CS/GridDataAttributes/Form1.cs#L23
gridControl1.MainView.PopulateColumns();
(gridControl1.MainView as GridView).BestFitColumns();
}
winforms-lookupedit-bind-to-simple-data-types/CS/LookupEdit-StandardBinding/Form1.cs#L49
gridView1.Columns["CategoryID"].ColumnEdit = riLookup;
gridView1.BestFitColumns();
}
winforms-grid-bind-to-business-objects/CS/GridBoundToRuntimeCreatedData/Form1.cs#L47
gridView1.BestFitColumns();
}
scheduledJobsGrid.DataSource = scheduledJobs
scheduledJobsView.BestFitColumns()
End Sub
winforms-grid-bind-to-azure-sql-database/VB/DXApplication1/Main.vb#L16
gridControl.DataSource = Await DataTableProvider.Instance.GetDataAsync()
gridView.BestFitColumns()
gridView.LoadingPanelVisible = False
winforms-grid-data-annotation-attributes/VB/GridDataAttributes/Form1.vb#L20
gridControl1.MainView.PopulateColumns()
TryCast(gridControl1.MainView, GridView).BestFitColumns()
End Sub
winforms-lookupedit-bind-to-simple-data-types/VB/LookupEdit-StandardBinding/Form1.vb#L53
gridView1.Columns("CategoryID").ColumnEdit = riLookup
gridView1.BestFitColumns()
End Sub
winforms-grid-bind-to-business-objects/VB/GridBoundToRuntimeCreatedData/Form1.vb#L49
gridView1.BestFitColumns()
End Sub
See Also