Question

I'm creating a HtmlTable with a for loop and I want to know why I can't modify the cells for each row.

for(int i=0; i<10; i++)
{
   row = new HtmlTableRow();
   mainTbl.Rows.Add(row);
}

Whenever I try the code below I receive an out of index range error. Why?

mainTbl.Rows[0].Cells[1].Text = "A"
mainTbl.Rows[1].Cells[2].Text = "B"
mainTbl.Rows[2].Cells[3].Text = "B"
Was it helpful?

Solution

I think,you must add cells to rows before adding them to collection. Something like this:

for(int i=0; i<10; i++)
{
   row = new HtmlTableRow();
   for(int j=0;j<3;++j)
   {
      row.Cells.Add(new HtmlTableCell());
   }
   mainTbl.Rows.Add(row);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top