Columns are vertical groups of cells in a spreadsheet. They are listed from top to bottom in the worksheet and labelled with alphabetical characters, such as A, B, C, etc. Note that a specific column can be accessed by its index or its name.
The following sections explain various operations on columns that you can perform in the Spread for WPF.
You can insert one or more columns at any position in a worksheet using the InsertColumns method the IWorksheet interface.
The following example code inserts three columns starting from the second position in the worksheet:
You can remove one or more columns from a worksheet using the RemoveColumns method of the IWorksheet interface.
The following example code removes three columns starting with the column at the third position.
You can clear the unwanted contents of a single column or multiple columns from the control. To accomplish this, use the IWorksheet's Columns property to reference a collection of columns, and then apply the Clear method to clear the contents of a specific column or to clear the contents of all columns.
The following example code clears the content of the columns.
You can move columns by dragging when the value of the DragDropMode enumeration is set to Column. By default, the value is none.
To move a column, click on the header of the column you want to move and then drag and drop the header to the desired position as shown in the gif below.
The following example code moves a column as shown in the above image.
You can set the number of columns to be displayed in a worksheet using the ColumnCount property of the IWorksheet interface.
The following example code sets the number of columns to three in a worksheet as shown in the above image.
All columns in a worksheet have the same size by default. To set the width of a specific column in a worksheet, use the ColumnWidth property of the IRange interface. The column width is defined in pixels.
The following example code sets the width of the column B to 190 pixels.
You can adjust the size of a column in the worksheet by using the Resizable property of the IRange interface. This property allows users to change column width by clicking and dragging columns' border based on the content size.
To resize a column width:
By default, resizing column widths is enabled. However, you can prevent resizing by setting the Resizable property to false. This property can be set in both XAML view and in the code view.
The following example code disables resizing columns.
By default, automatic column width adjustment is enabled. When you move the mouse over the border of a column header, the mouse pointer changes to a resizing shape. Double-clicking on the column edge resizes the column to fit the large content of that column to preferred width. To autofit multiple columns, select them, and double click any boundary between two column headers in the selection.
Additionally, you can also specify the automatic fit behavior using the AutoFit method for the columns.
The following example code autofits data in the columns.
You can automatically merge adjacent cells in the same column when their values are equal. To enable this feature, set the MergePolicy enumeration for a specific column, which has the following values.
Value | Description |
None | Do not merge automatically. |
Always | Merge adjacent cells whenever their values are equal. |
Restricted | Adjacent cells are merged if their values are equal and the corresponding ranges in the previous column are merged. |
The following code sample sets auto-merging for columns 1 and 2 as shown in the above image.
You can hide or show columns in a worksheet by using the Hidden property of the IRange interface. If the property value is set to true, it hides the column. To show the column again, change the property value to false.
The following example code hides the second column in the worksheet as shown in the above image.
You can easily apply alternate colors to the columns of your worksheet by using the AlternatingColumns property of the IWorksheetStyles interface. Setting the alternate column colors can improve the readability of your worksheet.
To use this functionality, ensure that the Enabled property of the IAlternatingStyles interface is set to true, which allows the alternate column colors to be applied. You can choose a color from the color list by setting the ThemeColor property of the IInterior interface. The ThemeColor property accepts values from the ThemeColors enumeration.
The following example code sets different colors to the alternate columns as shown in the above image.