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.
Document document = server.Document;
document.BeginUpdate();
// Create a new table style.
TableStyle tStyleMain = document.TableStyles.CreateNew();
// Specify style characteristics.
tStyleMain.AllCaps = true;
tStyleMain.FontName = "Segoe Condensed";
tStyleMain.FontSize = 14;
tStyleMain.Alignment = ParagraphAlignment.Center;
tStyleMain.TableBorders.InsideHorizontalBorder.LineStyle = TableBorderLineStyle.Dotted;
tStyleMain.TableBorders.InsideVerticalBorder.LineStyle = TableBorderLineStyle.Dotted;
tStyleMain.TableBorders.Top.LineThickness = 1.5f;
tStyleMain.TableBorders.Top.LineStyle = TableBorderLineStyle.Double;
tStyleMain.TableBorders.Left.LineThickness = 1.5f;
tStyleMain.TableBorders.Left.LineStyle = TableBorderLineStyle.Double;
tStyleMain.TableBorders.Bottom.LineThickness = 1.5f;
tStyleMain.TableBorders.Bottom.LineStyle = TableBorderLineStyle.Double;
tStyleMain.TableBorders.Right.LineThickness = 1.5f;
tStyleMain.TableBorders.Right.LineStyle = TableBorderLineStyle.Double;
tStyleMain.CellBackgroundColor = System.Drawing.Color.LightBlue;
tStyleMain.TableLayout = TableLayoutType.Fixed;
tStyleMain.Name = "MyTableStyle";
//Add the style to the document.
document.TableStyles.Add(tStyleMain);
document.EndUpdate();
document.BeginUpdate();
// Create a table.
Table table = document.Tables.Create(document.Range.Start, 3, 3);
table.TableLayout = TableLayoutType.Fixed;
table.PreferredWidthType = WidthType.Fixed;
table.PreferredWidth = DevExpress.Office.Utils.Units.InchesToDocumentsF(4.5f);
table[1, 1].PreferredWidthType = WidthType.Fixed;
table[1, 1].PreferredWidth = DevExpress.Office.Utils.Units.InchesToDocumentsF(1.5f);
// Apply a previously defined style.
table.Style = tStyleMain;
document.EndUpdate();

document.InsertText(table[1, 1].Range.Start, "STYLED");

The code execution result is illustrated below.

apiExample-CreateTableStyle