Skip to main content
All docs
V19.1
.NET Framework 4.5.2+
Row

StyleCollection.Add(String, Range) Method

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

Namespace: DevExpress.Spreadsheet

Assembly: DevExpress.Spreadsheet.v19.1.Core.dll

Declaration

Style Add(
    string name,
    Range range
)

Parameters

Name Type Description
name String

A style name.

range Range

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

Returns

Type Description
Style

The created cell style.

Remarks

Use the current 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 created 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.


// 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.
// ...

See how to create or modify a style.

Exceptions

System.ArgumentNullException

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

System.InvalidOperationException

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

See Also