Category / Section
How to fit the columns which are inserted at runtime in WinForms Spreadsheet (SfSpreadsheet)?
1 min read
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