Skip to main content Skip to footer

Resizing Column In Spread WPF

Spread Winforms provides a very useful feature of resizing a Cell to Fit the Data using GetPreferredColumnWidth method wherein 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. However, such method is missing from Spread WPF. Although we can resize the column to fit the longest string of text in the column by double clicking the right edge of the column header but how do we resize the column when Spread's CanUserResize property is set to false.

Still wondering how??

Let's see how we can do that with simple piece of code :) After populating the Spread WPF control, we need to set the size of the columns to fit the maximum width of the text using the following code:-


    double cellWidth = 0.0F;  
    for (int r = 0; r < Spread.ActiveSheet.Rows.Count; r++)  
        for (int c = 0; c < Spread.ActiveSheet.Columns.Count; c++)  
        {  
            var formattedText = new FormattedText(Spread.Sheets[0].Cells[r, c].Text, CultureInfo.CurrentUICulture,  
FlowDirection.LeftToRight, new Typeface("Arial"), FontSize = 14, System.Windows.Media.Brushes.Black);  
            if (cellWidth < formattedText.Width)  
            {  
                cellWidth = formattedText.Width;  
            }  
            else  
                Spread.ActiveSheet.Columns[c].Width = cellWidth;  
        }  

So this small code snippet completes the implementation process. To see it in action, you may download the sample with the complete implementation from below mentioned link. Download Sample

MESCIUS inc.

comments powered by Disqus