简体   繁体   English

如何以编程方式在每行中创建具有不同单元格数量的表?

[英]How to create table with different number of cell in each row programmatically?

I am a new ASP.NET developer. 我是新的ASP.NET开发人员。 I need to create a table with different number of cells in each row, so how to do that in C#? 我需要在每行中创建一个具有不同数量单元格的表,那么如何在C#中做到这一点?

I googled about it but I could not be able to find clear useful resource about this issue. 我在Google上进行了搜索,但无法找到有关此问题的清晰有用的资源。

Here's what I did in one of my projects: 这是我在一个项目中所做的工作:

private void AddBalancingLine(String balDescription, Decimal balAmount)
{
    TableRow tr = new TableRow();
    tr.Height = Unit.Pixel(17);
    tblDetailLines.Rows.Add(tr);

    tr.Cells.Add(newCell(""));
    tr.Cells.Add(newCell(""));
    tr.Cells.Add(newCell(""));
    tr.Cells.Add(newCell(balDescription, HorizontalAlign.Left, "normalFont", 8));
    tr.Cells.Add(newCell(nullAmount(balAmount), HorizontalAlign.Right, "normalFont", 2));
}

tblDetailLines is the System.Web.UI.WebControls.Table to which rows are being added. tblDetailLines是要向其中添加行的System.Web.UI.WebControls.Table The newCell function is overloaded with several signatures depending on how you want to create the cell: newCell函数根据您要如何创建单元而重载了多个签名:

private TableCell newCell(String cellText)
{
    return newCell(cellText, HorizontalAlign.Right, "normalFont");
}

private TableCell newCell(String cellText, HorizontalAlign alignment, String cssClass)
{
    return newCell(cellText, alignment, cssClass, 1);
}

private TableCell newCell(String cellText, HorizontalAlign alignment, String cssClass, Int32 colSpan)
{
    TableCell tc = new TableCell();
    tc.Text = cellText;
    tc.HorizontalAlign = alignment;
    tc.VerticalAlign = VerticalAlign.Top;
    tc.CssClass = cssClass;
    tc.ColumnSpan = colSpan;
    return tc;
}

The last newCell method is the one used in the last two calls to tr.Cells.Add in the main function -- this one allows you to specify the ColumnSpan for the cell. 最后一个newCell方法是在主函数中对tr.Cells.Add的最后两次调用中使用的方法-这个方法允许您指定单元格的ColumnSpan。 This table has 13 columns: 该表有13列:

  • 3 blank 3空白
  • 1 that spans 8 columns (description) 1个,跨越8列(描述)
  • 1 that spans 2 columns (amount) 1跨2列(数量)

Total is 13 columns. 总计13列。

Adjust the colSpan parameter when creating different rows to get different layouts. 创建不同的行以获取不同的布局时,请调整colSpan参数。

(For the very observant, the nullAmount function formats the balance amount, accounting for suppression of a 0 amount.) (对于非常细心的人, nullAmount函数设置余额金额的格式,以消除0金额。)

You can specify colspan for your cells, I wrote a little example to show you how to get a table with 2 rows, first one with 2 cells and second with 4 cells: 您可以为单元格指定colspan,我写了一个小例子来向您展示如何获取包含2行的表,第一个包含2个单元格,第二个包含4个单元格:

    Dim table As new Table 'Here is your table defined in markup, i declared to show an example
Dim row1 As new tablerow
Dim row2 As new tablerow
Dim cel1 As new TableCell With {.Text="row1 cel 1"}
Dim cel2 As new TableCell With {.Text="row1 cel 2"}
Dim cel3 As new TableCell With {.Text="row2 cel 1"}
Dim cel4 As new TableCell With {.Text="row2 cel 2"}
Dim cel5 As new TableCell With {.Text="row2 cel 3"}
Dim cel6 As new TableCell With {.Text="row2 cel 4"}

cel1.ColumnSpan=2
cel2.ColumnSpan=2
row1.Cells.AddRange({cel1,cel2})
row2.Cells.AddRange({cel3,cel4,cel5,cel6})
table.Rows.AddRange({row1,row2})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM