user1093651
user1093651

Reputation: 2021

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

I am a new ASP.NET developer. I need to create a table with different number of cells in each row, so how to do that in C#?

I googled about it but I could not be able to find clear useful resource about this issue.

Upvotes: 0

Views: 1889

Answers (2)

Piyey
Piyey

Reputation: 471

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:

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

Upvotes: 0

JeffK
JeffK

Reputation: 3039

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. The newCell function is overloaded with several signatures depending on how you want to create the cell:

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. This table has 13 columns:

  • 3 blank
  • 1 that spans 8 columns (description)
  • 1 that spans 2 columns (amount)

Total is 13 columns.

Adjust the colSpan parameter when creating different rows to get different layouts.

(For the very observant, the nullAmount function formats the balance amount, accounting for suppression of a 0 amount.)

Upvotes: 1

Related Questions