1

I would like to create a table with 5 columns. From there, I'll add cells as I see fit, automatically moving to the next row when the column is filled.I'm populating each cell with info from a row in a DataTable object. If you can imagine, the table will remain a fixed width, but grow in height as more items are added. Repeaters, maybe? I also have Telerik UI for AJAX, so if that route is a possibility, I'm open to it.

Code:

//create empty table with 5 columns here
foreach(DataRow row in table.Rows)
{
     TableCell cell = new TableCell();

     // I have the logic figured out to fill the cell here
     // Now, how to insert these into the table, automatically moving to the next row when necessary
}
1
  • I think you want to update the row where the cells are empty not insert. insert means adding new rows Commented Mar 21, 2014 at 23:45

1 Answer 1

2

You can do this:

    // Suppose you want to add 10 rows
    int totalRows = 10;
    int totalColumns = 5;

    for (int r = 0; r <= totalRows; r++)
    {
        TableRow tableRow = new TableRow();
        for (int c = 0; c <= totalColumns; c++)
        {
            TableCell cell = new TableCell();
            TextBox txtBox = new TextBox();
            txtBox.Text = "Row: " + r + ", Col:" + c;
            cell.Controls.Add(txtBox);
            //Add the cell to the current row.
            tableRow.Cells.Add(cell);

            if (c==5)
            {
                // This is the final column, add the row
                table.Rows.Add(tableRow);
            }
        }
    }

This way you can add any dynamic number of rows for your table, each row will contain your 5 columns.

Hope this helps.

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

1 Comment

Actually, I had the same idea! I am going to try this on Monday. Thank you.

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.