Back to Devexpress

StyleCollection.Add(String, CellRange) Method

officefileapi-devexpress-dot-spreadsheet-dot-stylecollection-dot-add-x28-system-dot-string-devexpress-dot-spreadsheet-dot-cellrange-x29.md

latest3.7 KB
Original Source

StyleCollection.Add(String, CellRange) Method

Creates a new style based on format settings of the specified cell range.

Namespace : DevExpress.Spreadsheet

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

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
Style Add(
    string name,
    CellRange range
)
vb
Function Add(
    name As String,
    range As CellRange
) As Style

Parameters

NameTypeDescription
nameString

A style name.

| | range | CellRange |

A cell range whose top left cell contains format settings for a new style.

|

Returns

TypeDescription
Style

The newly created cell style.

|

Exceptions

TypeDescription
ArgumentNullException

Occurs when a style name is empty or an object passed as the range parameter is null ( Nothing in Visual Basic).

| | InvalidOperationException |

Occurs when a style with the same name already exists in the collection.

|

Remarks

Use this Add method overload to create a new custom style based on existing cell formatting. Format settings are obtained from the top-left cell of the specified cell range. If the source cell has no formatting applied, the Spreadsheet creates a cell style with format settings identical to the Normal built-in style.

Take into account the following rules when you name a new style:

  • A style name must be unique. Use the StyleCollection.Contains method to ensure that the collection does not contain a style with the specified name.

  • A style name cannot be empty.

The new style has all StyleFlags set to true. To change the style’s format characteristics, modify the Style object’s properties within the Formatting.BeginUpdate - Formatting.EndUpdate method calls.

csharp
// Create a style based on format settings of the "B2" cell.
Style customStyle = workbook.Styles.Add("Custom Style", worksheet["B2"]);

// Modify the style's format characteristics if needed.
// ...
vb
' Create a style based on format settings of the "B2" cell.
Dim customStyle As Style = workbook.Styles.Add("Custom Style", worksheet("B2"))

' Modify the style's format characteristics if needed.
' ...

See how to create or modify a style.

See Also

StyleCollection Interface

StyleCollection Members

DevExpress.Spreadsheet Namespace