Skip to main content
A newer version of this page is available. .

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:

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.

    // 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();
class TableHelper
{
    public static void ChangeCellColor(TableCell cell, int i, int j)
    {
        cell.BackgroundColor = System.Drawing.Color.Yellow;
    }

    public static void ChangeCellBorderColor(TableCell cell, int i, int j)
    {
        cell.Borders.Bottom.LineColor = System.Drawing.Color.Red;
        cell.Borders.Left.LineColor = System.Drawing.Color.Red;
        cell.Borders.Right.LineColor = System.Drawing.Color.Red;
        cell.Borders.Top.LineColor = System.Drawing.Color.Red;
    }
}