Skip to main content
Row

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

StyleCollection.Add(String, CellRange) Method

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

Namespace: DevExpress.Spreadsheet

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

NuGet Package: DevExpress.Spreadsheet.Core

#Declaration

Style Add(
    string name,
    CellRange range
)

#Parameters

Name Type Description
name String

A style name.

range CellRange

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

#Returns

Type Description
Style

The newly created cell style.

#Exceptions

Type Description
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.

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

See Also