StyleCollection.Add(String) Method
Creates a new style with the specified name and appends it to the style collection.
Namespace: DevExpress.Spreadsheet
Assembly: DevExpress.Spreadsheet.v24.2.Core.dll
NuGet Package: DevExpress.Spreadsheet.Core
Declaration
Parameters
Name | Type | Description |
---|---|---|
name | String | A string that specifies the style name. |
Returns
Type | Description |
---|---|
Style | A Style object that specifies the new style. |
Remarks
A workbook’s style collection (IWorkbook.Styles) contains Microsoft® Excel® built-in styles by default. The BuiltInStyleId enumerator lists these predefined styles. You can use the Add method to extend the style collection with custom styles.
When you name a style, take into account the following restrictions:
- A style name must be unique. To ensure that StyleCollection does not contain a style with the specified name, use the StyleCollection.Contains method.
- A style name must be non-empty.
All settings of the created Style object except name (Style.Name) are the same as in the Normal default style. To set required formatting characteristics for the style, modify the corresponding properties of the Style object within the Formatting.BeginUpdate - Formatting.EndUpdate paired method calls.
To create a new style based on the existing style, use the Style.CopyFrom method.
For details on how to create and modify styles, see the How to: Create or Modify a Style document.
Example
Use the StyleCollection.Add
method of the Workbook.Styles collection to create a new style with a given name. The style’s format settings are identical to the Normal built-in style. Use the Style object’s properties to specify format characteristics for the new style.
// Add a new style under the "My Style" name to the style collection.
Style myStyle = workbook.Styles.Add("My Style");
// Specify the style's format characteristics.
myStyle.BeginUpdate();
try {
// Set the font color to blue.
myStyle.Font.Color = Color.Blue;
// Set the font size to 12.
myStyle.Font.Size = 12;
// Center text in cells.
myStyle.Alignment.Horizontal = SpreadsheetHorizontalAlignment.Center;
// Set the background fill.
myStyle.Fill.BackgroundColor = Color.LightBlue;
myStyle.Fill.PatternType = PatternType.LightGray;
myStyle.Fill.PatternColor = Color.Yellow;
}
finally {
myStyle.EndUpdate();
}
Related GitHub Examples
The following code snippet (auto-collected from DevExpress Examples) contains a reference to the Add(String) method.
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.