Master Master - 1 month ago 11
C# Question

How to populate second column of Table

I have a loop such that it creates table cells and rows and adds it to my table. The only issue i'm having is trying to add values specifically to my second column.

Here's the working code for 1 column

Table table = new Table();

foreach (var collateral in universalDTO.Collaterals)
{
Span span = new Span("First Column");
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(span);

TableCell cell = new TableCell();
cell.Blocks.Add(paragraph);

TableRow row = new TableRow();
row.Cells.Add(cell);

table.AddRow(row);
}


What I tried to do but it seems to just add values below instead of adding it to a new column on the right.

Table table = new Table();

foreach (var collateral in universalDTO.Collaterals)
{
Span span = new Span("First Column");
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(span);

Span span2 = new Span("Second Column");
Paragraph paragraph2 = new Paragraph();
paragraph.Inlines.Add(span2);

TableCell cell = new TableCell();
cell.Blocks.Add(paragraph);

TableCell cell2 = new TableCell();
cell2.Blocks.Add(paragraph2);

TableRow row = new TableRow();
row.Cells.Add(cell);
row.Cells.Add(cell2);
table.AddRow(row);
}


I also tried
table.AddColumn()
but that didn't work as well. Any help or suggestions would be great.

I'm trying to have my table look like below

Table:

First Column Second Column
First Column Second Column
First Column Second Column
First Column Second Column

Answer
Table table = new Table();

foreach (var collateral in universalDTO.Collaterals)
{
Span span = new Span("First Column");
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(span);

Span span2 = new Span("Second Column");
Paragraph paragraph2 = new Paragraph();
paragraph2.Inlines.Add(span2);

TableCell cell = new TableCell();
cell.Blocks.Add(paragraph);

TableCell cell2 = new TableCell();
cell2.Blocks.Add(paragraph2);

TableRow row = new TableRow();
row.Cells.Add(cell);
row.Cells.Add(cell2);
table.AddRow(row);
}