Autofit Cells in WPF GridControl
27 Feb 20255 minutes to read
GridControl provides support to autofit rows and columns based on the content of cells.
Autofit row height
GridControl provides the support to auto fit the row height based on content of the cells using ResizeRowsToFit method which accepts the following parameters,
-
GridRangeInfo - Specifies the range where
GridControl
auto fits the rows based on the cell content. - GridResizeToFitOptions - Specifies the auto fit settings to customize the auto fit behavior.
//To auto fit single row 2,
grid.Model.ResizeRowsToFit(GridRangeInfo.Row(2), GridResizeToFitOptions.NoShrinkSize);
//To auto fit range of rows from 3 to 6,
grid.Model.ResizeRowsToFit(GridRangeInfo.Rows(3,6), GridResizeToFitOptions.NoShrinkSize);
//To auto fit range of cell's(including Covered cells) row height,
this.grid.Model.ResizeRowsToFit(GridRangeInfo.Cells(1, 1, 2, 2),GridResizeToFitOptions.IncludeCellsWithinCoveredRange);
//To auto fit entire grid's row height,
this.grid.Model.ResizeRowsToFit(GridRangeInfo.Table(), GridResizeToFitOptions.None);
NOTE
Autofit column width
GridControl provides the support to auto fit the column width based on content of the cells using ResizeColumnsToFit method which accepts the following parameters,
-
GridRangeInfo - Specifies the range where
GridControl
auto fits the columns based on the cell content. - GridResizeToFitOptions - Specifies the auto fit settings to customize the auto fit behavior.
//To auto fit single column 2,
grid.Model.ResizeColumnsToFit(GridRangeInfo.Col(2), GridResizeToFitOptions.NoShrinkSize);
//To auto fit range of Columns from 3 to 6,
grid.Model.ResizeColumnsToFit(GridRangeInfo.Cols(3,6), GridResizeToFitOptions.NoShrinkSize);
//To auto fit range of cell's(including Covered cells) column width,
this.grid.Model.ResizeColumnsToFit(GridRangeInfo.Cells(1, 1, 2, 2),GridResizeToFitOptions.IncludeCellsWithinCoveredRange);
//To auto fit entire grid's column width,
this.grid.Model.ResizeColumnsToFit(GridRangeInfo.Table(), GridResizeToFitOptions.None);
NOTE
Autofit Options
The auto fitting of rows and columns can be customized by using GridResizeToFitOptions enum. This enum provides the following options to control the autofit behavior of cells.
Options | Description |
---|---|
None | Default option to autofit the cells. Also, this option while auto fitting, shrinks the size and does not include covered cells, header cells. |
ResizeCoveredCells | This option includes covered cells while auto fitting the cells. When using this option, only the last row or column of a covered range is resized. |
NoShrinkSize | This option autofit the cells without shrinking the original size.For example, while auto fitting the cells, if the size is reduced than the normal size of the cell due to the content, you can use this option to retain the original size without shrinking. |
IncludeHeaders | This option includes row/column header while auto fitting the cells. |
IncludeCellsWithinCoveredRange | `ResizeCoveredCells` option only autofit the last row or column of a covered range. But this option autofit the columns or rows before the last one also. |
IncludeHiddenCells | This option includes the hidden cells while auto fitting the cells. By default, visible cells only will be auto fitted and if you want to autofit all the cells including hidden cells in `GridControl`, then need to use this option. |
Autofit Cells based on Wrap Text
To autofit the cell’s height based on the applied wrap text, need to use ResizeRowsToFit method.
this.grid.Model[2, 2].TextWrapping = TextWrapping.Wrap;
this.grid.Model.ResizeRowsToFit(GridRangeInfo.Cell(2, 2),GridResizeToFitOptions.NoShrinkSize);
NOTE
How to avoid rendering issues due to fractions when auto fit the cells
When you autofit the cells, you may face scrolling and rendering issues in GridControl. You can set GridColumnAutoSizer.CanRoundCalculation to true
to avoid rendering issues if needed.
GridColumnAutoSizer.CanRoundCalculation = true;
Change the size of row height and column width to fit all cells in View
When GridControl is placed inside custom control and if you want to auto fit the row/column size of GridControl based on custom control resized position, then you can invoke SizeChanged event of GridControl and set the RowHeights and ColumnWidths property of GridModel to the resized height/width.
grid.SizeChanged += grid_SizeChanged;
//To autofit the cells based on custom control resizing,
void grid_SizeChanged(object sender, SizeChangedEventArgs e)
{
double width = (e.NewSize.Width - grid.Model.ColumnWidths.DefaultLineSize - 1) / (grid.Model.ColumnCount - 1);
double height = (e.NewSize.Height - grid.Model.RowHeights.DefaultLineSize - 1) / (grid.Model.RowCount - 1);
for (int i = 1; i < grid.Model.ColumnCount; ++i)
grid.Model.ColumnWidths[i] = width;
for (int j = 1; j < grid.Model.RowCount; ++j)
grid.Model.RowHeights[j] = height;
}
NOTE
See also
How to auto fit all the columns in a GridControl based on the Window size