Skip to main content
A newer version of this page is available. .

How to: Create, Modify And Delete Table Styles

  • 8 minutes to read

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.

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.

using DevExpress.Spreadsheet;
// ...

// Access the table style to be modified.
TableStyle tableStyle = workbook.TableStyles["tableStyleName"];

// Change the required formatting characteristics of the style elements.
tableStyle.BeginUpdate();
try {
    TableStyleElement wholeTable = tableStyle.TableStyleElements[TableStyleElementType.WholeTable];
    // wholeTable.Fill...
    // wholeTable.Borders...
    // wholeTable.Font...

    TableStyleElement tableHeader = tableStyle.TableStyleElements[TableStyleElementType.HeaderRow];
    // tableHeader.Fill.BackgroundColor...
    // tableHeader.Font...

    TableStyleElement firstColumn = tableStyle.TableStyleElements[TableStyleElementType.FirstColumn];
    // firstColumn.Clear();

    // ...
}
finally {
    tableStyle.EndUpdate();
}

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.

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.
Dim worksheet As Worksheet = workbook.Worksheets("Custom Table Style")
workbook.Worksheets.ActiveWorksheet = worksheet

' Access a table.
Dim table As Table = worksheet.Tables(0)

Dim styleName As String = "testTableStyle"

' If the style under the specified name already exists in the collection,
If workbook.TableStyles.Contains(styleName) Then
    ' apply this style to the table.
    table.Style = workbook.TableStyles(styleName)
Else
    ' Add a new table style under the "testTableStyle" name to the TableStyles collection.

    Dim customTableStyle_Renamed As TableStyle = workbook.TableStyles.Add("testTableStyle")

    ' Modify the required formatting characteristics of the table style. 
    ' Specify the format for different table elements.
    customTableStyle_Renamed.BeginUpdate()
    Try
        customTableStyle_Renamed.TableStyleElements(TableStyleElementType.WholeTable).Font.Color = Color.FromArgb(107, 107, 107)

        ' Specify formatting characteristics for the table header row. 
        Dim headerRowStyle As TableStyleElement = customTableStyle_Renamed.TableStyleElements(TableStyleElementType.HeaderRow)
        headerRowStyle.Fill.BackgroundColor = Color.FromArgb(64, 66, 166)
        headerRowStyle.Font.Color = Color.White
        headerRowStyle.Font.Bold = True

        ' Specify formatting characteristics for the table total row. 
        Dim totalRowStyle As TableStyleElement = customTableStyle_Renamed.TableStyleElements(TableStyleElementType.TotalRow)
        totalRowStyle.Fill.BackgroundColor = Color.FromArgb(115, 193, 211)
        totalRowStyle.Font.Color = Color.White
        totalRowStyle.Font.Bold = True

        ' Specify banded row formatting for the table.
        Dim secondRowStripeStyle As TableStyleElement = customTableStyle_Renamed.TableStyleElements(TableStyleElementType.SecondRowStripe)
        secondRowStripeStyle.Fill.BackgroundColor = Color.FromArgb(234, 234, 234)
        secondRowStripeStyle.StripeSize = 1
    Finally
        customTableStyle_Renamed.EndUpdate()
    End Try
    ' Apply the created custom style to the table.
    table.Style = customTableStyle_Renamed
End If

worksheet.Visible = True

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

SpreadsheetDocServer_CustomTableStyle

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.

Dim worksheet As Worksheet = workbook.Worksheets("Duplicate Table Style")
workbook.Worksheets.ActiveWorksheet = worksheet


' Access a table.
Dim table1 As Table = worksheet.Tables(0)
Dim table2 As Table = worksheet.Tables(1)

' Get the table style to be duplicated.
Dim sourceTableStyle As TableStyle = workbook.TableStyles(BuiltInTableStyleId.TableStyleMedium17)

' Duplicate the table style.
Dim newTableStyle As TableStyle = sourceTableStyle.Duplicate()

' Modify the required formatting characteristics of the created table style.
' For example, change background color of the table header.
newTableStyle.TableStyleElements(TableStyleElementType.HeaderRow).Fill.BackgroundColor = Color.FromArgb(&HA7, &HEA, &H52)

table1.Style = sourceTableStyle
table2.Style = newTableStyle

worksheet.Visible = True

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

SpreadsheetDocServer_DuplicateTableStyle

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.

See Also