How to: Change Table Color
- 2 minutes to read
The following example illustrates how to set the color of different table elements.
To achieve the required result, the following properties can be used:
- TableCell.BackgroundColor - sets the cell background color;
- TableCellBorder.LineColor - sets the cell border color;
- Table.TableBackgroundColor - sets the color of the empty space between cells.
The first two properties apply color settings to a single cell. To set them to the whole table, use the Table.ForEachCell method. It requires a delegate to execute in each cell of a table. The delegate is represented by the TableCellProcessorDelegate object.
Document document = server.Document;
// Create a table.
Table table = document.Tables.Create(document.Range.Start, 3, 5, AutoFitBehaviorType.AutoFitToWindow);
table.BeginUpdate();
// Provide the space between table cells.
// The distance between cells will be 4 mm.
document.Unit = DevExpress.Office.DocumentUnit.Millimeter;
table.TableCellSpacing = 2;
// Change the color of empty space between cells.
table.TableBackgroundColor = Color.Violet;
//Change cell background color.
table.ForEachCell(new TableCellProcessorDelegate(TableHelper.ChangeCellColor));
table.ForEachCell(new TableCellProcessorDelegate(TableHelper.ChangeCellBorderColor));
table.EndUpdate();
As a result, the table will look as illustrated below.