)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to fit the columns which are inserted at runtime in WinForms Spreadsheet (SfSpreadsheet)?

Platform: WinForms |
Control: Spreadsheet |
Published Date: August 1, 2016 |
Last Revised Date: January 24, 2020

Inserting columns

User can fit the column based on its content, which are inserted at runtime in SfSpreadsheet by calling AutofitColumns method for the inserted range and setting the column width using SetColumnWidth function of SpreadsheetGrid and then refresh the UI using InvalidateCell method like below code example

C#

/For inserting columns,
spreadsheet.ActiveSheet.InsertColumn( 2, 1, ExcelInsertOptions.FormatAsAfter);
spreadsheet.ActiveGrid.Model.InsertColumns(2, 1);
 
//For setting cell value,
var range = spreadsheet.ActiveSheet.Range[2, 2];
spreadsheet.ActiveGrid.SetCellValue(range, "This is a sample using Autofit");
 
//For Auto fit that particular column(2),
spreadsheet.ActiveSheet["B1:B100"].AutofitColumns();
var width = spreadsheet.ActiveSheet.GetColumnWidthInPixels(2);
spreadsheet.ActiveGrid.SetColumnWidth(2,2,width); 
 
//For refreshing the view   
spreadsheet.ActiveGrid.InvalidateCell(GridRangeInfo.Col(2));

Sample: WF

Reference link: https://help.syncfusion.com/windowsforms/spreadsheet/rows-and-columns

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon