Back to Devexpress

GridView.BestFitColumns() Method

windowsforms-devexpress-dot-xtragrid-dot-views-dot-grid-dot-gridview-f389693c.md

latest8.8 KB
Original Source

GridView.BestFitColumns() Method

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

Declaration

csharp
public virtual void BestFitColumns()
vb
Public Overridable Sub BestFitColumns

Remarks

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.

Specific Notes

Example

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

csharp
private void GridView1_CellValueChanged(object sender, CellValueChangedEventArgs e) {
  gridView1.BestFitColumns();
}
vb
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.

| |

GridOptionsView.BestFitMode

|

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.

report-server-how-to-manage-scheduled-jobs-from-a-windows-forms-application/CS/ScheduledTasksAPIClientDemo/MainForm.cs#L43

csharp
scheduledJobsGrid.DataSource = scheduledJobs;
    scheduledJobsView.BestFitColumns();
}

winforms-grid-bind-to-azure-sql-database/CS/DXApplication1/Main.cs#L14

csharp
gridControl.DataSource = await DataTableProvider.Instance.GetDataAsync();
gridView.BestFitColumns();
gridView.LoadingPanelVisible = false;

winforms-grid-data-annotation-attributes/CS/GridDataAttributes/Form1.cs#L23

csharp
gridControl1.MainView.PopulateColumns();
    (gridControl1.MainView as GridView).BestFitColumns();
}

winforms-lookupedit-bind-to-simple-data-types/CS/LookupEdit-StandardBinding/Form1.cs#L49

csharp
gridView1.Columns["CategoryID"].ColumnEdit = riLookup;
    gridView1.BestFitColumns();
}

winforms-grid-bind-to-business-objects/CS/GridBoundToRuntimeCreatedData/Form1.cs#L47

csharp
gridView1.BestFitColumns();
}

report-server-how-to-manage-scheduled-jobs-from-a-windows-forms-application/VB/ScheduledTasksAPIClientDemo/MainForm.vb#L43

vb
scheduledJobsGrid.DataSource = scheduledJobs
    scheduledJobsView.BestFitColumns()
End Sub

winforms-grid-bind-to-azure-sql-database/VB/DXApplication1/Main.vb#L16

vb
gridControl.DataSource = Await DataTableProvider.Instance.GetDataAsync()
gridView.BestFitColumns()
gridView.LoadingPanelVisible = False

winforms-grid-data-annotation-attributes/VB/GridDataAttributes/Form1.vb#L20

vb
gridControl1.MainView.PopulateColumns()
    TryCast(gridControl1.MainView, GridView).BestFitColumns()
End Sub

winforms-lookupedit-bind-to-simple-data-types/VB/LookupEdit-StandardBinding/Form1.vb#L53

vb
gridView1.Columns("CategoryID").ColumnEdit = riLookup
    gridView1.BestFitColumns()
End Sub

winforms-grid-bind-to-business-objects/VB/GridBoundToRuntimeCreatedData/Form1.vb#L49

vb
gridView1.BestFitColumns()
End Sub

See Also

Grid Columns

BestFit()

CalcColumnBestWidth(GridColumn)

GetColumnHeaderBestWidth

GetHeaderBestWidth()

GridView Class

GridView Members

DevExpress.XtraGrid.Views.Grid Namespace