Skip to main content
All docs
V24.1

GridControl.CalcBestSize(Size, Boolean) Method

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.v24.1.dll

NuGet Packages: DevExpress.Win.Grid, DevExpress.Win.Navigation

Declaration

public Size CalcBestSize(
    Size maxSize,
    bool checkScrollInfo = false
)

Parameters

Name Type Description
maxSize Size

The maximum allowed size.

Optional Parameters

Name Type Default Description
checkScrollInfo Boolean False

true to include the size of scrollbars into the calculation; otherwise, false.

Returns

Type Description
Size

The optimal grid size required to fit its content.

Remarks

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

You can also call the GridView.BestFitColumns method to resize grid columns to the optimal width.

Usage Notes

  • The CalcBestSize method supports only GridView and its descendants.
  • The CalcBestSize method ignores group row content and the Group Panel width.
  • In master-detail mode, the 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.

See Also