如何填充表的第二列

时间:2016-11-09 18:04:03

标签: c#

我有一个循环,它创建表格单元格和行并将其添加到我的表格中。我唯一的问题是尝试将值专门添加到我的第二列。

这里是1列

的工作代码
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);
}

我尝试做什么,但似乎只是添加下面的值,而不是将其添加到右侧的新列。

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);
}

我也试过了table.AddColumn(),但那也不行。任何帮助或建议都会很棒。

我试图让我的桌子看起来像下面

Table:

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

1 个答案:

答案 0 :(得分:1)

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);
}