Spread Windows Forms 11.0 Product Documentation
Resizing a Cell to Fit the Data
Spread Windows Forms 11.0 Product Documentation > Developer's Guide > Customizing Row, Column, and Cell Appearance > Customizing the Appearance of a Cell > Resizing a Cell to Fit the Data

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 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

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 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.

Besides getting the height for a row with the GetPreferredRowHeight method, you can get a width for a column using the GetPreferredColumnWidth 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.

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.

C#
Copy Code
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()); 
VB
Copy Code
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