Click to See Complete Forum and Search --> : Dynamic TableRow


rpcarnell
04-12-2007, 03:13 AM
Here's what I want to do: I created an ASP:Table and now I want to add rows dynamically using TableRow. Thing is I want to have 3 pics on every row, which means that there must be a new tablerow every time the integer iced is a number that can be divided by 3 (iced % 3).



The code below would be simple and straightforward, but one of the problems with C# is that it doesn't recognize the TableRow tRow = new Table(); outside if {}, and if I declare TableRow outside the if, all I get is one tablerow for all the pictures (if it is declared above while{} ), or a tablerow for all pics with number divisible by 3 (if declared inside the while but outside the if).



How do I solve this problem?



while (myReader.Read())
{



if (iced ==1 || (iced%3) == 0)
{
contents = contents + iced.ToString();
Label1.Text = contents.ToString();
TableRow tRow = new TableRow();
value1.Rows.Add(tRow);

}



TableCell tCell = new TableCell();
tCell.Text = myReader.GetString(1);
tRow.Cells.Add(tCell);



iced++;

}

________________________________________________________
http://www.carbotek.org

PeOfEo
04-12-2007, 10:15 AM
tRow must be outside of the if statement, otherwise you have a visibility issue, because in the next bit of code you are adding cells to tRow. You can define tRow inside the if if you want, but all references to it must be inside the if as well otherwise you are likely to get scope errors. Try bringing the next three lines up into the if statement and see what that does for you.