Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Building Controls / September 2003

Tip: Looking for answers? Try searching our database.

Iteration over TableRowCollection -> list [...] modified exception

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ben Schwehn - 25 Sep 2003 21:13 GMT
Hello everyone!

I have a control that derives from WebControls.Table. This control has a
method CreateTable() that builds a simple table. I also have second
control that, again, derives from Table. This control instantiates an
instance of the first control and then i want to copy all the rows of this
control  into this.Rows like this:

TableControl1 tc1 = new TableControl1(); tc1.CreateTable(); foreach
(TableRow tr in tc1.Rows)
    this.Rows.Add(tr);

And what i get is a exception:

The list that this enumerator is bound to has been modified. An enumerator
can only be used if the list doesn't change.

i don't quite understand what happens here. Apparently Rows.Add somehow
changes the original RowCollection...

Could anyone tell me what happens and why?

Also, what is the recommended way to copy all rows from one table into
another table. Ideally i don't want a copy of the original rows to be
made, I just want to put the references to the orignial rows into the
second table's Row collection.

I added a complete sample that shows this behaviour below.
Thanks a lot  :o)

Ben

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace tester
{

    [ToolboxData("<{0}:TableControl1 runat=server></{0}:TableControl1>")]

    public class TableControl1 : System.Web.UI.WebControls.Table    {
        protected override void CreateChildControls() {
            CreateTable();
        }
        public void CreateTable() {
            TableRow tr = new TableRow();
            TableCell td = new TableCell();
            td.Text = "test";
            tr.Cells.Add(td);
            this.Rows.Add(tr);
            TableRow tr2 = new TableRow();
            TableCell td2 = new TableCell();
            td2.Text = "test2";
            tr2.Cells.Add(td2);
            this.Rows.Add(tr2);
            TableRow tr3 = new TableRow();
            TableCell td3 = new TableCell();
            td3.Text = "test3";
            tr3.Cells.Add(td3);
            this.Rows.Add(tr3);
        }
    }
    public class TableControl2 : System.Web.UI.WebControls.Table {
        protected override void CreateChildControls() {
            TableControl1 tc1 = new TableControl1();
            tc1.CreateTable();
            foreach(TableRow tr in tc1.Rows)
                this.Rows.Add(tr);
        }
    }
}
Ben Schwehn - 26 Sep 2003 11:37 GMT
> Also, what is the recommended way to copy all rows from one table into
> another table. Ideally i don't want a copy of the original rows to be
> made, I just want to put the references to the orignial rows into the
> second table's Row collection.

propbably not a good idea because control has a field owner and other
things that have to be changed
I use an array and Copyto now and it seems to work

ben

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.