windowsforms-devexpress-dot-xtragrid-dot-gridcontrol-dot-calcbestsize-x28-system-dot-drawing-dot-size-system-dot-boolean-x29.md
Calculates the grid size required to fit its content or to display the maximum number of rows and columns. The size cannot exceed the specified maximum size.
Namespace : DevExpress.XtraGrid
Assembly : DevExpress.XtraGrid.v25.2.dll
NuGet Packages : DevExpress.Win.Grid, DevExpress.Win.Navigation
public Size CalcBestSize(
Size maxSize,
bool checkScrollInfo = false
)
Public Function CalcBestSize(
maxSize As Size,
checkScrollInfo As Boolean = False
) As Size
| Name | Type | Description |
|---|---|---|
| maxSize | Size |
The maximum allowed size.
|
| Name | Type | Default | Description |
|---|---|---|---|
| checkScrollInfo | Boolean | False |
true to include the size of scrollbars into the calculation; otherwise, false.
|
| Type | Description |
|---|---|
| Size |
The optimal grid size required to fit its content.
|
You can use the CalcBestSize method to resize the grid to its optimal size. In this case, you must call this method twice: to calculate the grid size and to include scrollbars in the calculation (they may appear/disappear after the first resize).
gridControl1.Size = gridControl1.CalcBestSize(new System.Drawing.Size(1600, 1200), false);
gridView1.LayoutChanged();
gridControl1.Size = gridControl1.CalcBestSize(new System.Drawing.Size(1600, 1200), true);
gridView1.LayoutChanged();
gridControl1.Size = gridControl1.CalcBestSize(New System.Drawing.Size(1600, 1200), False)
gridView1.LayoutChanged()
gridControl1.Size = gridControl1.CalcBestSize(New System.Drawing.Size(1600, 1200), True)
gridView1.LayoutChanged()
You can also call the GridView.BestFitColumns method to resize grid columns to the optimal width.
CalcBestSize method supports only GridView and its descendants.CalcBestSize method ignores group row content and the Group Panel width.CalcBestSize method calculates the optimal width only for the master view.The following code snippet (auto-collected from DevExpress Examples) contains a reference to the CalcBestSize(Size, 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.
winforms-grid-change-height-to-match-height-of-rows/CS/GridControlAutoSize/Form1.cs#L33
Size maxSize = new Size(ClientSize.Width, ClientSize.Height);
gridControl1.Size = gridControl1.CalcBestSize(maxSize, false);
gridControl1.MainView?.LayoutChanged();
winforms-grid-change-height-to-match-height-of-rows/VB/GridControlAutoSize/Form1.vb#L35
Dim maxSize As New Size(ClientSize.Width, ClientSize.Height)
gridControl1.Size = gridControl1.CalcBestSize(maxSize, False)
gridControl1.MainView?.LayoutChanged()
See Also