Table
The Table element is a Block element that provides a grid-based organization. It accepts TableRow objects as children. The TableRow object contains TableCell.
Inserting a Table
When creating an instance of the Table class, pass the document that the table belongs to as a parameter to the constructor. You can add tables as a child of a BlockContainer element—Section, TableCell, Headers and Footers—through the Blocks collection.
The code snippet from Example 1 creates and inserts a Table to a Section.
Example 1: Create and insert a table to a section
Table emptyTable = new Table(document); // Table object with 0 rows and 0 columns.
section.Blocks.Add(emptyTable);
Table table = new Table(document, 10, 5); // Table object with 10 rows and 5 columns.
section.Blocks.Add(emptyTable);
The parent
BlockContainerelement (in this case, theSection) must belong to the same document that is passed to the constructor of theTable.
You can add a table at a specific index in the Blocks collection of a BlockContainer by using the Insert() method. Example 2 shows how to add a table at the beginning of a section.
Example 2: Insert a table at a specific index
Table table1 = new Table(document, 10, 5);
section.Blocks.Insert(0, table1);
You can also use the AddTable() method of the Blocks collection of a BlockContainer. The method creates a new Table instance, adds it to the container, and returns it.
Example 3: Insert a new table to a container
Table table2 = section.Blocks.AddTable();
You can also insert a new Table in the document with the RadFlowDocumentEditor class.
Example 4: Insert a table using RadFlowDocumentEditor
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(new RadFlowDocument());
Table table3 = editor.InsertTable(5, 3);
Modifying a Table
The Table element exposes several properties that allow you to customize the layout of the elements placed underneath it. Some of these properties are style properties, and some of the values represent a themable object.
Style properties are properties that can be inherited from a style. For more information about styles, see Style Properties. Themable objects are objects that can be inherited from a theme. For more information about themes, see Document Themes.
-
Properties: Gets all table properties as aTablePropertiesobject. For more information on how to use table properties, see the Style Properties article. -
Rows: Represents theTableRowCollectionof theTable. -
StyleId: Represents the ID of the style applied to theTableelement. -
Alignment: Specifies the alignment of theTable. This is a style property. -
Borders: Specifies the borders of theTable. This is a style property. -
Shading: Represents the shading applied to the table. It is a composite object and is read-only. You can obtain the following properties from it:BackgroundColor: Specifies the background color for the shading. This is a style property. The value is a themable object.PatternColor: Specifies the pattern color for the shading. This is a style property. The value is a themable object.Pattern: Specifies the pattern that is used to lay the pattern color over the background color for the shading. This is a style property.
-
GridColumnsCount: Returns the number of columns in the table grid. -
GridRowsCount: Returns the number of rows in the table grid. -
TableCellSpacing: Specifies the spacing between adjacent cells and the edges of the table. The value is in device independent pixels (1/96 inch). This is a style property. -
HasCellSpacing: Indicates whetherTableCellSpacingis applied in the table. -
TableCellPadding: Specifies the default padding of the cells inside the table. This is a style property. -
Indent: Represents the size of the indent added before the leading edge of the table. The value is in device independent pixels (1/96 inch). This is a style property. -
FlowDirection: Represents the flow direction of cells inside the table. The default value isLeftToRight. This property cannot be derived from a style. -
PreferredWidth: Specifies the preferred width of the table. This property cannot be derived from a style. -
Looks: Specifies which components of the conditional style to apply, if one exists. This property cannot be derived from a style. -
LayoutType: Specifies the algorithm that is used to lay out the contents of this table. The possible values areFixedWidthorAutoFit. The default isAutoFit. This property cannot be derived from a style. -
Overlap: Indicates whether this floating table allows other floating tables to overlap its extents. This property cannot be derived from a style.
Operating with a Table
Creating a Table with Content
Example 5 demonstrates how to add a Table with 5 rows and 10 columns to a RadFlowDocument.
Example 5: Create a table with content and add it to a RadFlowDocument
RadFlowDocument document = new RadFlowDocument();
Table table = document.Sections.AddSection().Blocks.AddTable();
document.StyleRepository.AddBuiltInStyle(BuiltInStyleNames.TableGridStyleId);
table.StyleId = BuiltInStyleNames.TableGridStyleId;
ThemableColor cellBackground = new ThemableColor(Colors.Gray);
for (int i = 0; i < 5; i++)
{
TableRow row = table.Rows.AddTableRow();
for (int j = 0; j < 10; j++)
{
TableCell cell = row.Cells.AddTableCell();
cell.Blocks.AddParagraph().Inlines.AddRun(string.Format("Cell {0}, {1}", i, j));
cell.Shading.BackgroundColor = cellBackground;
cell.PreferredWidth = new TableWidthUnit(50);
}
}
See Also
- Table API Reference
- Section
- TableRow
- TableCell
- Style Properties
- Finding a Table Containing a Specific Bookmark in Word Documents
- Generating Dynamic DOCX Documents with Tables and CheckBoxes Using RadWordsProcessing
- Creating Headers with Left, Center, and Right Text/Image in Exported PDF Documents Using RadWordsProcessing
- How to Handle Missing Content in Nested Tables While Converting DOCX to PDF Format