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

How to: Create, Modify and Delete Table Styles

  • 7 minutes to read

A workbook contains a collection of table styles to format tables (see the How to: Apply a Table Style to a Table example). The IWorkbook.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 IWorkbook.TableStyles collection by the table style name.

    Note

    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 to a Table document.

Create Your Own Custom Table Style

  • Create a New Table Style

    This example demonstrates how to create a custom style to format tables.

    1. Add a new table style to the IWorkbook.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).

      Note

      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.
    // Access a table.
    Table table = worksheet.Tables[0];
    
    String styleName = "testTableStyle";
    
    // If the style under the specified name already exists in the collection,
    if (workbook.TableStyles.Contains(styleName))
    {
        // 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.
        TableStyle customTableStyle = workbook.TableStyles.Add("testTableStyle");
    
        // Modify the required formatting characteristics of the table style. 
        // Specify the format for different table elements.
        customTableStyle.BeginUpdate();
        try
        {
            customTableStyle.TableStyleElements[TableStyleElementType.WholeTable].Font.Color = Color.FromArgb(107, 107, 107);
    
            // Specify formatting characteristics for the table header row. 
            TableStyleElement headerRowStyle = customTableStyle.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. 
            TableStyleElement totalRowStyle = customTableStyle.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.
            TableStyleElement secondRowStripeStyle = customTableStyle.TableStyleElements[TableStyleElementType.SecondRowStripe];
            secondRowStripeStyle.Fill.BackgroundColor = Color.FromArgb(234, 234, 234);
            secondRowStripeStyle.StripeSize = 1;
        }
        finally
        {
            customTableStyle.EndUpdate();
        }
        // Apply the created custom style to the table.
        table.Style = customTableStyle;
    }
    

    The following image shows the table formatted with the custom table style created by the code above.

    TableExample_CustomStyle

  • 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 removing the table header row formatting copied from the source table style.

    Note

    A complete sample project is available at https://github.com/DevExpress-Examples/how-to-format-tables-e4909

    TableStyle DuplicateAndModifyTableStyle(IWorkbook workbook, string sourceStyleName) {
        // Get the table style to be duplicated.
        TableStyle sourceTableStyle = workbook.TableStyles[sourceStyleName];
    
        // Duplicate the table style.
        TableStyle newTableStyle = sourceTableStyle.Duplicate();
    
        // Modify the required formatting characteristics of the created table style.
        // For example, remove exisitng formatting from the header row element.
        newTableStyle.TableStyleElements[TableStyleElementType.HeaderRow].Clear();
    
        return newTableStyle;
    }
    

    The image below shows the TableStyleMedium21 built-in table style and its modified copy.

    TableExample_DuplicateStyle

Delete Table Styles

To remove a table style from the IWorkbook.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).

Note

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