# Resizing the Row or Column to Fit the Data

Learn how to resize rows and columns to fit data in your WinForms application with ease.

## Content

Spread for WinForms also allows you to resize the column width or row height based on the length or breadth of data in the cells in that column or row. The size of the row or column with the largest data is referred to as the preferred size.
The methods that make use of the preferred size are:

* [Row](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.Row.html) class, [GetPreferredHeight](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.Row.GetPreferredHeight.html) method
* [Column](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.Column.html) class, [GetPreferredWidth](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.Column.GetPreferredWidth.html) method
* [SheetView](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.SheetView.html) class, [GetPreferredRowHeight](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.SheetView.GetPreferredRowHeight.html) method
* [SheetView](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.SheetView.html) class, [GetPreferredColumnWidth](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.SheetView.GetPreferredColumnWidth.html) method
* [SheetView](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.SheetView.html) class, [GetPreferredCellSize](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.SheetView.GetPreferredCellSize.html) method

The [GetPreferredHeight](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.Row.GetPreferredHeight.html) method of the [Row](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.Row.html) class and [GetPreferredWidth](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.Column.GetPreferredWidth.html) method of the [Column](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.Column.html) class always include the header cells. The overloaded [GetPreferredColumnWidth](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.SheetView.GetPreferredColumnWidth.html) method of the [SheetView](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.SheetView.html) class has one overload that always includes the header cells while another overload allows you to choose whether to include or exclude header cells. In the following code, `width1` and `width2` include the header cells but `width3` excludes the header cells.

```csharp
float width1 = fpspread.Sheets[0].Columns[0].GetPreferredWidth();
float width2 = fpspread.Sheets[0].GetPreferredColumnWidth(0);
float width3 = fpspread.Sheets[0].GetPreferredColumnWidth(0, true);
```

For information on setting the cell size based on the size of the data, refer to [Resizing a Cell to Fit the Data](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-ssobject-cell/spwin-celldata/spwin-cellfitdata).
For information on allowing the user to resize the data, refer to [Allowing the User to Resize Rows or Columns](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-ssobject-rowcol/spwin-rowcolsize/spwin-rowcol-userresize).

> type=note
> Note:
>
> * By default, the WordWrap of the header cell is set to true. If the header text is longer than the cell text, you should disable the WordWrap like the following example code to make the maximum length (the width of the widest text) match the header text.
>
>     ```csharp
>     //Disable WordWrap for all column header labels
>     FarPoint.Win.Spread.CellType.ColumnHeaderRenderer ch = new FarPoint.Win.Spread.CellType.EnhancedColumnHeaderRenderer();
>     ch.WordWrap = false;
>     fpSpread1.ActiveSheet.ColumnHeader.DefaultStyle.Renderer = ch;
>     ```
>
>     ```vbnet
>     'Disable WordWrap for all column header labels
>     Dim ch As New FarPoint.Win.Spread.CellType.EnhancedColumnHeaderRenderer
>     ch.WordWrap = False
>     FpSpread1.ActiveSheet.ColumnHeader.DefaultStyle.Renderer = ch
>     ```
> * The column size automatically adjusted by double-clicking the border of column header, or the preferred size fetched by calling the GetPreferredColumnWidth method, etc. slightly differs depending on the setting font (font name, size, style) of the target cell.
> * In case of rich text type cell, auto-size is enabled, and when using multiple fonts, the column width is shifted by several pixels.The automatically adjusted column widths also differ depending on the used font, size, and style (bold, italic, and underline).(The text width fetched by the GetPreferredColumnWidth method is also different.)
> * The height of the row fetched by calling the GetPreferredRowHeight method in a rich text type cell differs depending on the used font, size, and style (bold, italic, underline). Also, when the [WordWrap](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.CellType.RichTextCellType.WordWrap.html) property or [Multiline](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.CellType.RichTextCellType.Multiline.html) property is set to True, the required row height cannot be fetched if different font information is mixed in the same cell, so in this case, you need to make fine adjustments.

## **Example**

This example sets the row height and column width.

```csharp
FarPoint.Win.Spread.Row row;
FarPoint.Win.Spread.Column col;
float sizerow;
float sizercol;
row = fpSpread1.ActiveSheet.Rows[0];
col = fpSpread1.ActiveSheet.Columns[0];
fpSpread1.ActiveSheet.Cells[0, 0].Text = "This text is used to determine the height and width.";
sizerow = row.GetPreferredHeight();
sizecol = col.GetPreferredWidth();
row.Height = sizerow;
col.Width = sizecol;
```

```vbnet
Dim row As FarPoint.Win.Spread.Row
Dim col As FarPoint.Win.Spread.Column
Dim sizerow As Single
Dim sizecol As Single
row = fpSpread1.ActiveSheet.Rows(0)
col = fpSpread1.ActiveSheet.Columns(0)
fpSpread1.ActiveSheet.Cells(0, 0).Text = "This text is used to determine the height and width."
sizerow = row.GetPreferredHeight()
sizecol = col.GetPreferredWidth()
row.Height = sizerow
col.Width = sizecol
```

## See Also

[Setting the Row Height or Column Width](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-ssobject-rowcol/spwin-rowcolsize)
[Allowing the User to Resize Rows or Columns](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-ssobject-rowcol/spwin-rowcolsize/spwin-rowcol-userresize)
[Using Auto Row Height](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-ssobject-rowcol/spwin-rowcolsize/Using-Auto-Row-Height)