How to: Merge and Split Table Cells
- 2 minutes to read
The following example illustrates how to merge and split table cells programmatically.
Merge Cells
To merge cells, use the Table.MergeCells method with the passed cells marking a range to be merged. As a result, all the cells falling to the stated range will be combined into a single cell. The content of all the merged cells will persist.
This code snippet performs the following:
- creates a table containing 6 rows and 8 columns
- merges cells horizontally in the third row from the third to the sixth column
- merges cells vertically in the third column from the fourth to the eighth row
Note
A complete sample project is available at https://github.com/DevExpress-Examples/winforms-richedit-document-api-e5219
Table table = document.Tables.Create(document.Range.Start, 6, 8);
table.BeginUpdate();
table.MergeCells(table[2, 1], table[5, 1]);
table.MergeCells(table[2, 3], table[2, 7]);
table.EndUpdate();
The image below illustrates the result of code execution.
Split Cells
The following code snippet splits a table cell into three by using the TableCell.Split method.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/winforms-richedit-document-api-e5219
Table table = document.Tables.Create(document.Range.Start, 3, 3, AutoFitBehaviorType.FixedColumnWidth, 350);
//split a cell to three:
table.Cell(2, 1).Split(1, 3);
The result is illustrated below.