1

I created a Table with these headers. Now How do I use a WHILE LOOP and add empty rows dynamically on the page load. Like if I want 40 empty row with 3 cells each, How do I use a while loop and add rows?

ASP.NET C# Microsoft Visual Studio

    TableRow tr = new TableRow();

    TableCell tcel = new TableCell();
    tcel.Text = "id";
    tr.Cells.Add(tcel);

    TableCell tcel1 = new TableCell();
    tcel1.Text = "Work";
    tr.Cells.Add(tcel1);

    TableCell tcel2 = new TableCell();
    tcel2.Text = "Email";
    tr.Cells.Add(tcel2);

    Table1.Rows.Add(tr);

2 Answers 2

1

Pretty simple. Append these:

var j = 0;
while (j++ < 40)
{
    var k = 0;
    var emptyRow = new TableRow();
    while (k++ < 3)
    {
        var emptyCell = new TableCell();
        emptyCell.Text = "|empty Cell|";
        emptyRow.Cells.Add(emptyCell);
    }
    Table1.Rows.Add(emptyRow);
}
Sign up to request clarification or add additional context in comments.

Comments

0
int rowCount = 0;  
while(rowCount < 40)
{
    TableRow tr = new TableRow();
    int cellCount = 0;
    while(cellCount < 3)
    {
        TableCell tc = new TableCell();
        tc.Text = "cellText";
        tr.Cells.Add(tc);
        cellCount++;
    }
    Table1.Rows.Add(tr);
    rowCount++;
}

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.