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

How to: Create and Apply Table Style

  • 2 minutes to read

This example describes how to create new table style and apply it to the table.

To create a new table style, do the following:

  1. Create a new TableStyle object using the TableStyleCollection.CreateNew method.
  2. Specify the style settings using the corresponding TableStyle properties.
  3. Add a new item in the table styles collection, represented by the TableStyleCollection instance. To do that, access the collection by the Document.TableStyles property and use the TableCellStyleCollection.Add method. Note that without adding a table style to the collection, you won’t be able to apply it.
  4. To apply the newly created style to the table, set the table’s Table.Style property to the corresponding object.

Note

A complete sample project is available at https://github.com/DevExpress-Examples/tables-simple-example-t472346

document.BeginUpdate();
//Create a new table style
TableStyle tStyleMain = document.TableStyles.CreateNew();

//Specify style options       
tStyleMain.TableBorders.InsideHorizontalBorder.LineStyle = TableBorderLineStyle.Single;
tStyleMain.TableBorders.InsideHorizontalBorder.LineColor = Color.White;

tStyleMain.TableBorders.InsideVerticalBorder.LineStyle = TableBorderLineStyle.Single;
tStyleMain.TableBorders.InsideVerticalBorder.LineColor = Color.White;
tStyleMain.CellBackgroundColor = Color.FromArgb(227, 238, 220);
tStyleMain.Name = "MyTableStyle";

//Add the style to the document collection
document.TableStyles.Add(tStyleMain);

//Create conditional styles (styles for specific table elements)         
TableConditionalStyle myNewStyleForOddRows = tStyleMain.ConditionalStyleProperties.CreateConditionalStyle(ConditionalTableStyleFormattingTypes.OddRowBanding);
myNewStyleForOddRows.CellBackgroundColor = Color.FromArgb(196, 220, 182);

TableConditionalStyle myNewStyleForBottomRightCell = tStyleMain.ConditionalStyleProperties.CreateConditionalStyle(ConditionalTableStyleFormattingTypes.BottomRightCell);
myNewStyleForBottomRightCell.CellBackgroundColor = Color.FromArgb(188, 214, 201);
document.EndUpdate();

document.BeginUpdate();

// Apply a previously defined style to the table
document.Tables[0].Style = tStyleMain;
document.EndUpdate();