officefileapi-devexpress-dot-spreadsheet-dot-stylecollection-dot-add-x28-system-dot-string-devexpress-dot-spreadsheet-dot-cellrange-x29.md
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
Style Add(
string name,
CellRange range
)
Function Add(
name As String,
range As CellRange
) As Style
| Name | Type | Description |
|---|---|---|
| name | String |
A style name.
| | range | CellRange |
A cell range whose top left cell contains format settings for a new style.
|
| Type | Description |
|---|---|
| Style |
The newly created cell style.
|
| 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.
|
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.
// ...
' 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