officefileapi-devexpress-dot-xtrarichedit-dot-api-dot-native-dot-tablestylecollection-dot-add-x28-devexpress-dot-xtrarichedit-dot-api-dot-native-dot-tablestyle-x29.md
Add a style to the style collection.
Namespace : DevExpress.XtraRichEdit.API.Native
Assembly : DevExpress.RichEdit.v25.2.Core.dll
NuGet Package : DevExpress.RichEdit.Core
void Add(
TableStyle style
)
Sub Add(
style As TableStyle
)
| Name | Type | Description |
|---|---|---|
| style | TableStyle |
A table style to add to the collection.
|
To create a new style, call the CreateNew method.
The code sample below creates a new table style and applies it to a table:
using DevExpress.Office.Utils;
using DevExpress.XtraRichEdit.API.Native;
using System.Drawing;
static void CreateAndApplyTableStyle(RichEditDocumentServer wordProcessor)
{
// Access a document.
Document document = wordProcessor.Document;
// Start to edit the document.
document.BeginUpdate();
// Create a new table style.
TableStyle tStyleMain = document.TableStyles.CreateNew();
// Specify table style options.
tStyleMain.AllCaps = true;
tStyleMain.FontName = "Segoe Condensed";
tStyleMain.FontSize = 14;
tStyleMain.Alignment = ParagraphAlignment.Center;
TableBorders tableBorders = tStyleMain.TableBorders;
tableBorders.InsideHorizontalBorder.LineStyle =
BorderLineStyle.Dotted;
tableBorders.InsideVerticalBorder.LineStyle =
BorderLineStyle.Dotted;
tableBorders.Top.LineThickness = 1.5f;
tableBorders.Top.LineStyle = BorderLineStyle.Double;
tableBorders.Left.LineThickness = 1.5f;
tableBorders.Left.LineStyle = BorderLineStyle.Double;
tableBorders.Bottom.LineThickness = 1.5f;
tableBorders.Bottom.LineStyle = BorderLineStyle.Double;
tableBorders.Right.LineThickness = 1.5f;
tableBorders.Right.LineStyle = BorderLineStyle.Double;
tStyleMain.CellBackgroundColor = Color.LightBlue;
tStyleMain.TableLayout = TableLayoutType.Fixed;
tStyleMain.Name = "MyTableStyle";
// Add the style to the collection of styles.
document.TableStyles.Add(tStyleMain);
// Finalize to edit the document.
document.EndUpdate();
// Start to edit the document.
document.BeginUpdate();
// Create a table with three rows and columns
// at the document range's start position.
Table table = document.Tables.Create(document.Range.Start, 3, 3);
// Set the table width to a fixed value.
table.TableLayout = TableLayoutType.Fixed;
table.PreferredWidthType = WidthType.Fixed;
table.PreferredWidth = Units.InchesToDocumentsF(3.5f);
// Set the cell width to a fixed value.
table[1, 1].PreferredWidthType = WidthType.Fixed;
table[1, 1].PreferredWidth = Units.InchesToDocumentsF(1.5f);
// Apply the created style to the table.
table.Style = tStyleMain;
// Finalize to edit the document.
document.EndUpdate();
// Insert text to the table cell.
document.InsertText(table[1, 1].Range.Start, "STYLED");
}
Dim document As Document = server.Document
document.BeginUpdate()
' Create a new table style.
Dim tStyleMain As TableStyle = document.TableStyles.CreateNew()
' Specify style characteristics.
tStyleMain.AllCaps = True
tStyleMain.FontName = "Segoe Condensed"
tStyleMain.FontSize = 14
tStyleMain.Alignment = ParagraphAlignment.Center
tStyleMain.TableBorders.InsideHorizontalBorder.LineStyle = BorderLineStyle.Dotted
tStyleMain.TableBorders.InsideVerticalBorder.LineStyle = BorderLineStyle.Dotted
tStyleMain.TableBorders.Top.LineThickness = 1.5F
tStyleMain.TableBorders.Top.LineStyle = BorderLineStyle.Double
tStyleMain.TableBorders.Left.LineThickness = 1.5F
tStyleMain.TableBorders.Left.LineStyle = BorderLineStyle.Double
tStyleMain.TableBorders.Bottom.LineThickness = 1.5F
tStyleMain.TableBorders.Bottom.LineStyle = BorderLineStyle.Double
tStyleMain.TableBorders.Right.LineThickness = 1.5F
tStyleMain.TableBorders.Right.LineStyle = BorderLineStyle.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.
Dim table As Table = document.Tables.Create(document.Range.Start, 3, 3)
table.TableLayout = TableLayoutType.Fixed
table.PreferredWidthType = WidthType.Fixed
table.PreferredWidth = Units.InchesToDocumentsF(4.5F)
table(1, 1).PreferredWidthType = WidthType.Fixed
table(1, 1).PreferredWidth = Units.InchesToDocumentsF(1.5F)
' Apply a previously defined style.
table.Style = tStyleMain
document.EndUpdate()
document.InsertText(table(1, 1).Range.Start, "STYLED")
The following code snippets (auto-collected from DevExpress Examples) contain references to the Add(TableStyle) method.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.
winforms-richedit-document-api/CS/RichEditAPISample/CodeExamples/Table.cs#L128
//Add the style to the document.
document.TableStyles.Add(tStyleMain);
document.EndUpdate();
wpf-richedit-document-api/CS/DXRichEditControlAPISample/CodeExamples/TableActions.cs#L96
//Add the style to the document.
document.TableStyles.Add(tStyleMain);
document.EndUpdate();
winforms-richedit-tables-simple-example/CS/TablesSimpleExample/Form1.cs#L214
// Add the style to the document collection
document.TableStyles.Add(tStyleMain);
word-document-api-table-examples/CS/Program.cs#L200
//Add the style to the document collection
document.TableStyles.Add(tStyleMain);
winforms-richedit-document-api/VB/RichEditAPISample/CodeExamples/Table.vb#L116
'Add the style to the document.
document.TableStyles.Add(tStyleMain)
document.EndUpdate()
wpf-richedit-document-api/VB/DXRichEditControlAPISample/CodeExamples/TableActions.vb#L86
'Add the style to the document.
document.TableStyles.Add(tStyleMain)
document.EndUpdate()
winforms-richedit-tables-simple-example/VB/TablesSimpleExample/Form1.vb#L176
' Add the style to the document collection
document.TableStyles.Add(tStyleMain)
' Create conditional styles (styles for specific table elements)
word-document-api-examples/VB/CodeExamples/TablesActions.vb#L205
myNewStyleForEvenColumns.CellBackgroundColor = System.Windows.Forms.ControlPaint.LightLight(System.Drawing.Color.PaleVioletRed)
document.TableStyles.Add(myNewStyle)
' Create a new table with four rows and columns at the document range's end position.
word-document-api-table-examples/VB/Program.vb#L161
'Add the style to the document collection
document.TableStyles.Add(tStyleMain)
'Create conditional styles (styles for specific table elements)
See Also