# Resizing a Cell to Fit the Data

Learn how to resize cells based on data length and optimize the display of text content in your spreadsheets.

## Content

You can resize the cell based on the length of the data in the cell. The size of the cell with the largest data is called the preferred size.
The SheetView [GetPreferredCellSize](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.SheetView.GetPreferredCellSize.html) method retrieves the preferred size of the specified cell.
This figure shows the result of the example code that resizes the column based on the text in the cells of that column.
![Cell Width Fits Text](https://cdn.mescius.io/document-site-files/images/2238e618-a1fb-45a6-9ce5-9a4157bd2931/images/cell-width-resized.png)
Some cell types ignore the size parameter while other cell types use the size parameter. For example, a single line text cell type ignores the size parameter while a word-wrapping multiple-line text cell type uses the size parameter's width property to determine line breaks. Basically, the size parameter's width (or height) is used in determining wrapping breaks for horizontal (or vertical) content. The sheet's [GetPreferredRowHeight](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.SheetView.GetPreferredRowHeight.html) method passes the cell's current size to the cell renderer's **GetPreferredSize** methods and it assumes that you want multiple-line text cells to expand vertically using the cell's current width. For more information on how cell types display data, refer to [Understanding How Cell Types Display and Format Data](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-setcelltypes/spwin-datacelltextvalue).
Besides getting the height for a row with the [GetPreferredRowHeight](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.SheetView.GetPreferredRowHeight.html) method, you can get a width for a column using the [GetPreferredColumnWidth](/spreadnet/api/latest/online-win/FarPoint.Win.Spread/FarPoint.Win.Spread.SheetView.GetPreferredColumnWidth.html) method.
For information on setting an entire row or column of cells based on the size of the data, refer to [Resizing the Row or Column to Fit the Data](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-ssobject-rowcol/spwin-rowcolsize/spwin-rowcolresizefit).

## Using Code

Set the width of the cell to the value of the preferred size to show the entire contents of the cell.

## Example

After setting the text in the contents, resize the column width of the cell to match the maximum size of the cell.

```csharp
System.Drawing.Size sz;
fpSpread1.ActiveSheet.SetValue(0, 0, "Expand the cell to fit the text.");
sz = fpSpread1.ActiveSheet.GetPreferredCellSize(0,0);
fpSpread1.ActiveSheet.Columns[0].Width = sz.Width;
MessageBox.Show("The width of the cell is " + sz.Width.ToString());
```

```vbnet
Dim sz As System.Drawing.Size
fpSpread1.ActiveSheet.SetValue(0, 0, "Expand the cell to fit the text.")
sz = fpSpread1.ActiveSheet.GetPreferredCellSize(0, 0)
fpSpread1.ActiveSheet.Columns(0).Width = sz.Width
MessageBox.Show("The width of the editor is " & sz.Width.ToString())
```

## See Also

[Resizing the Data to Fit the Cell](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-ssobject-cell/spwin-celldata/spwin-appearshrink)
[Allowing Cell Data to Overflow](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-ssobject-cell/spwin-celldata/spwin-celloflow)
[Aligning Cell Contents](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-ssobject-cell/spwin-celldata/spwin-cellalign)