Groups | Blog | Home
all groups > asp.net building controls > november 2004 >

asp.net building controls : Building a table from within a loop


Grant
11/30/2004 9:39:05 PM
Hi,

Ive got this loop within a loop and Im trying to build a table. Problem is
when I exit the 2nd foreach loop (having created a new row) I cant add the
new row because it isnt in scope anymore. Basically I have this custom
arraylist containing a name property which has multiple attributes. I loop
through the values and foreach value I do a 2nd loop to get all the values
attributes. So I guess my question is how would I build a table with 2
columns? 1st column containing the value and the 2nd column containing the
values? so it should look something like:

property1 | property attribute
| property attribute
| property attribute
Property2 | property2 attribute
Property2 | property2 attribute
etc...

Heres my code:
-----------------------------------------------------
foreach ( CustomArrayList _object in _GlobalPropertyValues )
{ //Create a table cell for user names
usernameCell = new TableCell();
usernameCell.BorderStyle = BorderStyle.Solid;
usernameCell.Text = _object.propertyName;

foreach ( string _propertyName in _object )
{ //Create another table cell for user AD paths
userContainerCell = new TableCell();
userContainerCell.BorderStyle = BorderStyle.Solid;
userContainerCell.Text = _propertyName;

//Construct the table rows
newRow = new TableRow();
newRow.Cells.Add( userContainerCell );
tblResults.Rows.Add( newRow );
}
//Add the cells to the table row
newRow = new TableRow();
newRow.Cells.Add( usernameCell );
//newRow.Cells.Add( userContainerCell );
tblResults.Rows.Add( newRow );
}
-----------------------------------------------------

The code above produces a single column table.


Thanks for any help on this!

Cheers,
Grant

Sergio Florez M.
12/1/2004 7:55:55 AM
When I dinamically create tables I don't add the TableRows to the table's
Rows collection, I add them directly to its Controls collection. Same thing
with TableCells to TableRows. Never had a problem using this approach so
maybe you should try it.

--
Sergio Florez M.
Medellín, Colombia.


[quoted text, click to view]

AddThis Social Bookmark Button