Back to Devexpress

CellRange.ColumnWidthInCharacters Property

officefileapi-devexpress-dot-spreadsheet-dot-cellrange-70af4dd4.md

latest13.4 KB
Original Source

CellRange.ColumnWidthInCharacters Property

Gets or sets the width of columns that contain the cell range in characters of the default font specified by the built-in Normal style.

Namespace : DevExpress.Spreadsheet

Assembly : DevExpress.Spreadsheet.v25.2.Core.dll

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
double ColumnWidthInCharacters { get; set; }
vb
Property ColumnWidthInCharacters As Double

Property Value

TypeDescription
Double

A Double value that is the number of characters that can be accommodated into the column width when the column is formatted with the built-in Normal style.

|

Remarks

To set or obtain the column width in units of measurement used in the workbook, use the CellRange.ColumnWidth property.

If columns containing the cell range have different widths, the ColumnWidthInCharacters and CellRange.ColumnWidth properties return the width of the left column.

Example

This example demonstrates how to control the column width in a worksheet.

Set the Width for Individual Columns

To specify the column width in characters of the default font specified by the built-in Normal style, use the Column.WidthInCharacters or CellRange.ColumnWidthInCharacters property.

To specify the column width in pixels, use the column’s Column.WidthInPixels property.

To set the column width in other measurement units, set the Workbook.Unit property to the required DocumentUnit enumeration member and specify the Column.Width or CellRange.ColumnWidth property.

Note

If the column width is set to 0, the column is hidden. You can also use the Column.Visible property to hide a column or display the hidden column again.

AutoFit Column Width

To automatically change the column width to fit the contents, use the columns’s Column.AutoFit method. To quickly autofit several columns in a worksheet, call the ColumnCollection.AutoFit method.

Match the Width of Two Columns

To match one column width to another, you can use the Column.Width property or the CellRange.CopyFrom method of the column object.

Set the Default Column Width

To set the default width for worksheet columns, use the Worksheet.DefaultColumnWidthInCharacters, Worksheet.DefaultColumnWidthInPixels or Worksheet.DefaultColumnWidth property. These properties do not affect columns with an explicitly specified width.

Set the Width for All Columns in a Worksheet

To change the width of all columns in a worksheet, use the CellRange.ColumnWidthInCharacters or CellRange.ColumnWidth property of the Row object corresponding to any row in a worksheet. This sets the width of all columns to the same value, overriding all previously applied column width settings.

View Example

csharp
// Set the "B" column width to 30 characters of the default font that is specified by the Normal style.
worksheet.Columns["B"].WidthInCharacters = 30;

// Set the "C" column width to 15 millimeters.
workbook.Unit = DevExpress.Office.DocumentUnit.Millimeter;
worksheet.Columns["C"].Width = 15;

// Set the width of the column that contains the "E15" cell to 100 points.
workbook.Unit = DevExpress.Office.DocumentUnit.Point;
worksheet.Cells["E15"].ColumnWidth = 100;

// Set the width of all columns that contain the "F4:H7" cell range (the "F", "G" and "H" columns) to 70 points.
worksheet.Range["F4:H7"].ColumnWidth = 70;

// Set the "J" column width to the "B" column width value.
worksheet.Columns["J"].Width = worksheet.Columns["B"].Width;

// Copy the "C" column width value and assign it to the "K" column width.
worksheet.Columns["K"].CopyFrom(worksheet.Columns["C"], PasteSpecial.ColumnWidths);

// Set the default column width to 40 pixels.
worksheet.DefaultColumnWidthInPixels = 40;
vb
' Set the "B" column width to 30 characters of the default font that is specified by the Normal style.
worksheet.Columns("B").WidthInCharacters = 30

' Set the "C" column width to 15 millimeters.
workbook.Unit = DevExpress.Office.DocumentUnit.Millimeter
worksheet.Columns("C").Width = 15

' Set the width of the column that contains the "E15" cell to 100 points.
workbook.Unit = DevExpress.Office.DocumentUnit.Point
worksheet.Cells("E15").ColumnWidth = 100

' Set the width of all columns that contain the "F4:H7" cell range (the "F", "G" and "H" columns) to 70 points.
worksheet.Range("F4:H7").ColumnWidth = 70

' Set the "J" column width to the "B" column width value.
worksheet.Columns("J").Width = worksheet.Columns("B").Width

' Copy the "C" column width value and assign it to the "K" column width.
worksheet.Columns("K").CopyFrom(worksheet.Columns("C"), PasteSpecial.ColumnWidths)

' Set the default column width to 40 pixels.
worksheet.DefaultColumnWidthInPixels = 40

The following code snippets (auto-collected from DevExpress Examples) contain references to the ColumnWidthInCharacters property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-spreadsheetcontrol-api-part1/CS/SpreadsheetControl/SpreadsheetActions/WorksheetActions.cs#L74

csharp
workbook.Worksheets["Sheet1"].Cells.FillColor = Color.LightSteelBlue;
workbook.Worksheets["Sheet1"].Cells["A1"].ColumnWidthInCharacters = 50;
workbook.Worksheets["Sheet1"].Cells["A1"].Value = "Sheet1's Content";

wpf-spreadsheetcontrol-api-part-1/CS/SpreadsheetControl_WPF_API/SpreadsheetActions/WorksheetActions.cs#L88

csharp
workbook.Worksheets["Sheet1"].Cells.FillColor = Color.LightSteelBlue;
workbook.Worksheets["Sheet1"].Cells["A1"].ColumnWidthInCharacters = 50;
workbook.Worksheets["Sheet1"].Cells["A1"].Value = "Sheet1's Content";

spreadsheet-document-api-examples-part1/CS/SpreadsheetExamples/SpreadsheetActions/ImportActions.cs#L22

csharp
static void ImportArrays(Workbook workbook) {
    workbook.Worksheets[0].Cells["A1"].ColumnWidthInCharacters = 35;
    workbook.Worksheets[0].Cells["A1"].Value = "Import an array horizontally:";

winforms-spreadsheetcontrol-api-part-2/CS/SpreadsheetControl_API_Part02/SpreadsheetActions/CustomFunctionActions.cs#L35

csharp
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Range["A1:H1"].ColumnWidthInCharacters = 12;
worksheet.Range["A1:H1"].Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center;

wpf-spreadsheetcontrol-api-part-2/CS/SpreadsheetControl_WPF_API_Part02/SpreadsheetActions/CustomFunctionActions.cs#L31

csharp
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Range["A1:H1"].ColumnWidthInCharacters = 12;
worksheet.Range["A1:H1"].Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center;

spreadsheet-document-api-examples-part1/VB/SpreadsheetExamples/SpreadsheetActions/ImportActions.vb#L28

vb
Private Sub ImportArrays(ByVal workbook As Workbook)
    workbook.Worksheets(0).Cells("A1").ColumnWidthInCharacters = 35
    workbook.Worksheets(0).Cells("A1").Value = "Import an array horizontally:"

winforms-spreadsheetcontrol-api-part1/VB/SpreadsheetControl/SpreadsheetActions/WorksheetActions.vb#L77

vb
workbook.Worksheets("Sheet1").Cells.FillColor = Color.LightSteelBlue
workbook.Worksheets("Sheet1").Cells("A1").ColumnWidthInCharacters = 50
workbook.Worksheets("Sheet1").Cells("A1").Value = "Sheet1's Content"

wpf-spreadsheetcontrol-api-part-1/VB/SpreadsheetControl_WPF_API/SpreadsheetActions/WorksheetActions.vb#L81

vb
workbook.Worksheets("Sheet1").Cells.FillColor = Color.LightSteelBlue
workbook.Worksheets("Sheet1").Cells("A1").ColumnWidthInCharacters = 50
workbook.Worksheets("Sheet1").Cells("A1").Value = "Sheet1's Content"

winforms-spreadsheetcontrol-api-part-2/VB/SpreadsheetControl_API_Part02/SpreadsheetActions/CustomFunctionActions.vb#L36

vb
Dim worksheet As Worksheet = workbook.Worksheets(0)
worksheet.Range("A1:H1").ColumnWidthInCharacters = 12
worksheet.Range("A1:H1").Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center

wpf-spreadsheetcontrol-api-part-2/VB/SpreadsheetControl_WPF_API_Part02/SpreadsheetActions/CustomFunctionActions.vb#L32

vb
Dim worksheet As Worksheet = workbook.Worksheets(0)
worksheet.Range("A1:H1").ColumnWidthInCharacters = 12
worksheet.Range("A1:H1").Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center

See Also

ColumnWidth

Width

WidthInCharacters

WidthInPixels

DefaultColumnWidth

RowHeight

How to: Specify Row Height or Column Width

CellRange Interface

CellRange Members

DevExpress.Spreadsheet Namespace