views:

48

answers:

3

I am adding texboxes into a table (type Table) but I can't add them. I can't add more than one cell to each row, any idea?

TextBox[] tx = new TextBox[10];
        TableCell[] tc = new TableCell[10];

        TableRow[] tr = new TableRow[10];

        for (int i = 0; i < 10; i++)
        {
            tx[i] = new TextBox();
            tc[i] = new TableCell();
            tc[i].Controls.Add(tx[i]);
        }

        for (int i = 0; i < 10; i++)
        {
            tr[i] = new TableRow();
            tr[i].Cells.Add(tc[i]);
        }

        for (int i = 0; i < 10; i++)
            Table1.Rows.Add(tr[i]);

It comes out like 10 rows each having only 1 cell

+1  A: 

Because you need an inner loop on this:

for (int i = 0; i < 10; i++)
{
    tr[i] = new TableRow();
    tr[i].Cells.Add(tc[i]);
}

Try this:

for (int i = 0; i < 10; i++)
{
    tr[i] = new TableRow();
    for (int x = 0; x < 10; x++)
    {
       tr[i].Cells.Add(tc[x]);
    }
}
Jason
now I got 10 cells but only one row!!
Ahmad Farid
A: 

Your loops are not set up to give you a 10x10 table

Table table = new Table();
TableRow tr = null;
TableCell tc = null;
for (int i = 0; i < 10; i++)
{
    tr = new TableRow();

    for (int j = 0; j < 10; j++)
    {
        tc = new TableCell();

        tc.Controls.Add(new TextBox());

        tr.Cells.Add(tc);
    }

    table.Rows.Add(tr);
}
BigBlondeViking
A: 

The cells has to be distinct: I need to create 100 cells not only 10!

TextBox[] tx = new TextBox[100];
        TableCell[] tc = new TableCell[100];

        TableRow[] tr = new TableRow[10];

        for (int i = 0; i < 100; i++)
        {
            tx[i] = new TextBox();
            tc[i] = new TableCell();
            tc[i].Controls.Add(tx[i]);
        }

        int x = 0;
        for (int i = 0; i < 10; i++)
        {
            tr[i] = new TableRow();
            for (int j=0; j < 10; j++)
            {
                tr[i].Cells.Add(tc[x++]);
            }
        }


        for (int i = 0; i < 10; i++)
            Table1.Rows.Add(tr[i]);
Ahmad Farid
its better to be done by a two-dimensional array though
Ahmad Farid