[Expand]General Information
[Expand]WinForms Controls
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Collapse]Office File API
  Demo Application
  Redistribution and Deployment
 [Collapse]Spreadsheet Document API
   Product Structure
   Getting Started
  [Expand]Spreadsheet Document
   Supported Formats
  [Expand]Cell Basics
  [Expand]Formulas
   Defined Names
   Data Binding
  [Expand]Pivot Tables
  [Expand]Charts and Graphics
  [Expand]Mail Merge
  [Collapse]Examples
   [Expand]Files
   [Expand]Workbooks
   [Expand]Worksheets
   [Expand]Rows and Columns
   [Expand]Cells
   [Expand]Formulas
   [Expand]Import and Export Data
   [Expand]Data Binding
    Sorting
   [Expand]Mail Merge
    Search
   [Expand]Shapes
   [Expand]Charts
   [Expand]Sparklines
   [Expand]Formatting Cells
   [Expand]Conditional Formatting
   [Expand]Group Data
   [Expand]Filter Data
   [Collapse]Tables
     How to: Create a Table
     How to: Perform Calculations in a Table
     How to: Apply a Table Style
     How to: Create, Modify And Delete Table Styles
   [Expand]Pivot Tables
   [Expand]Printing
   [Expand]Pictures
   [Expand]Protection
 [Expand]Word Processing Document API
 [Expand]PDF Document API
 [Expand]Excel Export Library
 [Expand]Snap Report API
 [Expand]Zip Compression and Archive API
 [Expand]Barcode Generation API
 [Expand]Unit Conversion API
 [Expand]API Reference
[Expand]Reporting
[Expand]Report and Dashboard Server
[Expand]Dashboard
[Expand]eXpressApp Framework
[Expand]eXpress Persistent Objects
[Expand]CodeRush
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

How to: Create, Modify And Delete Table Styles

A workbook contains a collection of table styles to format tables (see the How to: Apply a Table Style example). The Workbook.TableStyles property returns the TableStyleCollection object, which specifies the workbook's collection of table styles. By default, this collection contains built-in table styles similar to Microsoft® Excel® and the None table style, which specifies that no formatting should be applied to the table. If you create custom table styles, they are also placed in the workbook's table style collection.

You can do the following to manage the workbook's collection of table styles.

Expanded Modify Existing Table Styles

A table style (TableStyle) consists of a collection of table style elements (TableStyle.TableStyleElements). Each table style element (TableStyleElement) specifies formatting for a particular element of a table. The TableStyleElementType enumerator lists the supported table style element types.

Use properties of the TableStyleElement object to customize borders (TableStyleElement.Borders), fill (TableStyleElement.Fill) and font (TableStyleElement.Font) for the corresponding table element. If you wish to create a style providing striped row or column formatting for a table, customize the FirstRowStripe, SecondRowStripe, FirstColumnStripe or SecondColumnStripe table style elements and set their TableStyleElement.StripeSize property, which specifies the banding rule.

Thus, to modify a table style, follow the steps below.

  1. Access the table style to be changed. To do this, get the corresponding TableStyle object from the Workbook.TableStyles collection by the table style name.

    Important

    Built-in table styles cannot be modified. To check whether a table style is built-in or custom, use the TableStyle.BuiltIn property.

  2. Call the TableStyle.BeginUpdate method.
  3. Access the table style element to be modified from the TableStyle.TableStyleElements collection by the corresponding TableStyleElementType enumeration member. Use the TableStyleElement properties to specify the required formatting for the element. If you need to remove existing formatting from the element, use its TableStyleElement.Clear method.

    Repeat this step for all table style elements you wish to modify.

  4. Call the TableStyle.EndUpdate method.

After a table style is changed, the style modifications are automatically applied to all tables that use this style. For details on how a table style is applied to a table and table elements, review the How to: Apply a Table Style document.

Expanded Create Your Own Custom Table Style

Create a New Table Style

The example below demonstrates how to create a custom style to format tables.

  1. Add a new table style to the Workbook.TableStyles collection by calling the TableStyleCollection.Add method with the table style name passed as a parameter. This method returns the TableStyle object that represents the newly created table style. This object's TableStyle.Name property is set to the specified name. Other settings are identical to the None table style (the default built-in table style that specifies no formatting for a table).

    Important

    Note that table styles have unique names in the collection. To ensure that there is no table style under the specified name in the collection, use the TableStyleCollection.Contains method.

  2. Modify elements of the created table style (TableStyle.TableStyleElements) within the TableStyle.BeginUpdate and TableStyle.EndUpdate paired methods.

The following image shows the table formatted with the custom table style created by the code above (the workbook is opened in Microsoft® Excel®).

Duplicate an Existing Table Style

You can create new custom table styles based on existing table styles (for example, based on built-in table styles). To do this, use the TableStyle.Duplicate method. This method creates a copy of the specified style and returns the TableStyle object representing the newly created style. You can use properties of this object to change the style format settings.

For example, the code snippet below demonstrates how to duplicate an existing style and modify the new style a bit by changing the color of the header row at the top of the table.

The image below shows the TableStyleMedium17 built-in table style and its modified copy (the workbook is opened in Microsoft® Excel®).

Expanded Delete Table Styles

To remove a table style from the Workbook.TableStyles collection, use the TableStyleCollection.Remove method. After a table style is deleted, all tables to which this style is applied will be formatted with the default style (TableStyleCollection.DefaultStyle).

Important

Built-in table styles cannot be removed. To check whether a table style is built-in or custom, use the TableStyle.BuiltIn property.

Expanded See Also

Is this topic helpful?​​​​​​​