0

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.

4
  • 2
    And how do you want the result to look? Commented Dec 19, 2011 at 19:36
  • Your question might be about Jagged Arrays. See msdn.microsoft.com/en-us/library/2s05feca.aspx Commented Dec 19, 2011 at 19:36
  • need more information. sounds like you would need to use the repeater and have it logically build it but not enough data presented to give proper evaluation of your problem. Commented Dec 19, 2011 at 19:37
  • Can you post an example data set you would like displayed in the table? Also, do you want shorter rows to stretch to be the same width as longer rows, or just have blank space at the right hand side of shorter rows? Commented Dec 19, 2011 at 19:42

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

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:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.