Spread Windows Forms 17
Spread Windows Forms 17.0 Product Documentation / Developer's Guide / Rows and Columns / Setting the Row Height or Column Width / Resizing the Row or Column to Fit the Data
In This Topic
    Resizing the Row or Column to Fit the Data
    In This Topic

    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:

    The GetPreferredHeight method of the Row class and GetPreferredWidth method of the Column class always include the header cells. The overloaded GetPreferredColumnWidth method of the SheetView 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.

    C#
    Copy Code
    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.

    For information on allowing the user to resize the data, refer to Allowing the User to Resize Rows or Columns.

    • 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.
      C#
      Copy Code
      //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;
      
      Visual Basic
      Copy Code
      '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 property or Multiline 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.

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