CodeDon
CodeDon

Reputation: 77

Adding Row To a Table ASP.NET C# Microsoft Visual Studio

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

Upvotes: 1

Views: 6190

Answers (2)

Matt McHone
Matt McHone

Reputation: 11

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++;
}

Upvotes: 0

mshsayem
mshsayem

Reputation: 18028

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

Upvotes: 1

Related Questions