This project has moved. For the latest updates, please go here.

Preserving Paragraph Style in New Table Rows

Nov 17, 2014 at 11:55 AM
Edited Nov 17, 2014 at 11:56 AM
Hi,

I am working on populating a template which contains some tables. Part of the code creates a new row and then populate the cells with values from data source.

I have something like this:

First add the row:
Table myTable = this.theDoc.Tables[tableIdx];
Row newRow = myTable.InsertRow(beforeRow);
Then populate it (I have a wrapper with functions to do so):
public void WriteToTable(int tableIdx, int row, params object[] values)
{
    tableIdx -= 1;
    row -= 1;

    if (this.theDoc.Tables.Count > tableIdx)
    {
        for (int i = 0; i < values.Length; i++)
        {
           var cell = this.theDoc.Tables[tableIdx].Rows[row].Cells[i];
           SetCellValue(cell, values[i], styleName);
        }
    }            
}

// set the cell value - if there is already a paragraph write to it otherwise create.
private void SetCellValue(Cell cell, object value)
{
    if (cell.Paragraphs.Count > 0)
    {
        cell.Paragraphs[0].InsertText(0, value.ToString());
    }
    else
    {
        cell.InsertParagraph(value.ToString());
    }
}
As you can see, the added text are in default formatting.

Image

How do I make it so that the styles/formatting are kept?

Thanks,

Tomer

P.S. I tried to do as mentioned here (TEXT) copying the style name from the first cell in table but that didn't change anything.