I think this problem is related to Reference Types, and my lack of understanding of these …
So I have dynamically created ASP.Net Tables (as in Web.UI.WebControls.Table, not the database variety)
These can have anything from one row with one cell with text, to a whole series of nested tables and controls, depending on the clients.
I need to loop through each TableRow, if a certain condition is met then I copy that row to a 2nd Table object. Here’s a simplified bit of the code.
Table xTblComplete = (passed in as parameter) // original & complete table
Table xTblTemp = new Table(); // gets built dynamically with specific rows
foreach (TableRow xThisRow in xTblComplete.Rows)
{
if (xThisRow.Cells.Count > 0)
{
if (certain condition met)
{
xTblTemp.Rows.Add(xThisRow);
}
}
}
Where I come unstuck is that the foreach (row in table.rows) throws an error when I try to add the TableRow to Table2. I get the error “Collection was modified; enumeration operation may not execute. ”
This makes sense, in that I should be making a COPY of that Table Row to add.
Can anyone advise on how this is done? I’ve scanned MSDN and the forums for general copying-of-reference types, but they all seem to point to using ICloneable , which I believe I’m unable to do as this isn’t my class.
Am hoping this is something small and fundamental I’m missing out on, thanks in advance.
Thanks Ulises, your answers were helpful, unfortunately the complexity of these tables procluded a simple loop & copy contents. By that I mean that there were cells that might possibly have had 5 levels of nested tables, with any number of web controls inside each. Yep, a CSS perfectionist would retch at the idea of so many nested tables, but it’s what had to be done!!
In the end I utilized a while(bln) loop, examining xTblComplete.Rows[0] each time.
If it met the condition , I would copy it to xTmpTable, which also removed it from xTblComplete.
If it failed the condition, I would remove it myself ( xTblComplete.Rows.Remove(xTblComplete.Rows[0]);
This way Rows[0] would always be the next to process.
After each Loop I checked the bln for more rows to process, if none then the loop would exit.