How to properly render DataTable in Word template using LINQ Reporting Engine

I’m trying to render a simple table in my template, but can’t get it quite right.
Here’s my code:

 var dataTable = CreateTestDataTable();
 var columnNames = dataTable.Columns.Cast<DataColumn>().Select(x => x.ColumnName).ToList();
 sectionData.Content.AddDataSource(dataTable, "table");
 sectionData.Content.AddDataSource(columnNames, "columnNames");

private static DataTable CreateTestDataTable()
    {
        var table = new DataTable();
        table.Columns.Add("Column1");
        table.Columns.Add("Column2");

        var row1 = table.NewRow();
        row1["Column1"] = "Value 1.1";
        row1["Column2"] = "Value 1.2";
        table.Rows.Add(row1);

        var row2 = table.NewRow();
        row2["Column1"] = "Value 2.1";
        row2["Column2"] = "Value 2.2";
        table.Rows.Add(row2);

        return table;
    }

Here’s the template I’m currently using with 3 attempts:


test template.docx (14.5 KB)

It renders as follows:

So it feels like I’m close, but there’s still some issues with empty cells/offset. Any idea how to fix?

@FrederickEskens

Please try to modify template A by cutting template syntax from the first cell of the first row and pasting it to the second cell of the first row.

That did the trick, thanks!

1 Like