Treeview issues

Posts   
 
    
firestar
User
Posts: 10
Joined: 22-Aug-2006
# Posted on: 22-Aug-2006 08:47:15   

Hi,

I'm using a treeview to show a list of sections. I wrote some recursive functions to take care of this. However when I try to update my entities i get a whole lot of issues to overcome:


        private void deleteSectionToolStripMenuItem_Click ( object sender , EventArgs e )
        {
            if ( tvSections.SelectedNode != null )
            {
                int SectionID = Convert.ToInt32( tvSections.SelectedNode.Tag );

                ProjectSectionEntity section = GetEntityFromProject( SectionID );
            
                if ( section != null )
                {
                    if ( MessageBox.Show( 
                                string.Format("This section has {0} childs, they will be moved to \"{1}\".", section.ProjectSectionChilds.Count, section.ProjectSection.Name),
                                              "Delete?" , MessageBoxButtons.YesNo ) == DialogResult.Yes )
                    {                   
                        // add childs to the parent
                        section.ProjectSection.ProjectSectionChilds.AddRange( section.ProjectSectionChilds );

                        /////// REMOVE ///////////
                        
                        // repopulate tree
                        PopulateSectionTree( );
                    }
                }
            }
        }

After I have called the .Count function, the collection appears to be empty? Im trying to rearrange the child entities to the parent entity, but whatever I try I do not seem to get it to work as I can not use the AddRange function as the childs collection has been changed? I tried using the prefetch path before get all data before editing but again after calling .Count my collection is empty.

Anyone know how to solve this?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 22-Aug-2006 09:06:20   

Please elaborate more. Also, I don't understand what the following line of code should do:

 section.ProjectSection.ProjectSectionChilds.AddRange( section.ProjectSectionChilds );

firestar
User
Posts: 10
Joined: 22-Aug-2006
# Posted on: 22-Aug-2006 09:15:27   

It is a self reference

Parent -> child -> child. It is a FK to a PK in the table.

So if i delete the middle child, i want to move all childs to the parent of the entity.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 22-Aug-2006 10:52:55   

Your code snippet is not very enlighting what you're doing. If you want to move entities from one parent to another, you should simply either: - add them to the parent's collection (e.g. add order to customer.Orders). This will dereference the existing reference automatically OR - set the reference in the child to a new parent. (e.g. order.Customer = newCustomer). This also will dereference the existing reference automatically.

Frans Bouma | Lead developer LLBLGen Pro
firestar
User
Posts: 10
Joined: 22-Aug-2006
# Posted on: 22-Aug-2006 12:56:22   

This is what I'm actually doing.

ProjectSectionEntity is the entity that has two properties:

ProjectSection is the parent entity ProjectSectionChilds is the childs collection (ProjectSectionCollection).

So what you are saying is what I'm doing, im adding the current childs to the parents child collection.


Root
 - Section 1
 - Section 2
     - Section 2.1
        - Section 2.1.1
     - Section 2.2

If i delete section 2.1, its child (section 2.1.1) should be moved to the childs collection of Section 2. Section 2.2 should remain in the collection, so setting a new reference wont work, I'll have to add it to the collection, and llbl wont let me do it as after I call the .Count function the collection seems to be "modified" and I cant call the AddRange function with it.

Jessynoo avatar
Jessynoo
Support Team
Posts: 296
Joined: 19-Aug-2004
# Posted on: 22-Aug-2006 17:57:30   

To my knowledge 1-n relation collection are readonly but you can update them by referencing the child entities.

So I would do something like: (quick pseudo code)

For Each childSection in section

childSection.ParentSection = section.ParentSection

End For