Search code examples
c#novacode-docx

DocX table, how to set the header background colour?


I am creating a word document using DocX. I want to put in a background colour for a table row. I can't find how to do it:

var text = string.Format("{0}. {1}", SectionNo, section);
var headerTable = Doc.AddTable(1, 1);
headerTable.SetWidths(new float[] {600});
headerTable.Design = TableDesign.LightList;
var para = headerTable.Rows[0].Cells[0].Paragraphs.First();
para.Append(text);
Doc.InsertTable(headerTable);

Solution

  • There are two ways of doing it,

    tbl.Design = TableDesign.TableGrid 
    

    using the TableGrid is just a grid but there are some styles in here that may be of use. I found them all ugly as hell.

    The other way would be doing it at the cell level which I have found to be the better way of doing it.

    tbl.Rows[0].Cells[0].FillColor = Color.FromName("Red")
    

    The only caveat with this is that you may need to change your font colour too.