This is a migrated thread and some comments may be shown as answers.

How to change the top-level items of a RadPanelBar

9 Answers 108 Views
PanelBar
This is a migrated thread and some comments may be shown as answers.
Ed
Top achievements
Rank 1
Ed asked on 18 Sep 2008, 03:55 PM
Hi,

It's very simple -- I have a RadPanelBar hierarchy of items with 3 top-level items say), and I want to write code to change this for a hierarchy that instead has the children (i.e. Level 1 nodes) of the first item as its top-level items.  In other words, I want to replace the hierarchy tree with a subtree starting one level down, in code.  This *must* be simple, surely; yet I can't seem to do it!  Having seen no obvious method to use, I have resorted to manually copying the subtree to a temporary RadPanelBar and then swapping it for the original one, but I keep running into this error:

Collection was modified; enumeration operation may not execute.

What is the recommended way to do this?

Cheers,

Ed Graham

9 Answers, 1 is accepted

Sort by
0
Ed
Top achievements
Rank 1
answered on 23 Sep 2008, 03:25 PM
Hi Telerik team,

Can anybody help with this?  Do I need to submit a support ticket?

Thanks,

Ed
0
Atanas Korchev
Telerik team
answered on 24 Sep 2008, 07:12 AM
Hi Ed,

I couldn't understand your goal. Could you please be more specific? Are you trying to achieve the case demonstrated in this example?

The error you are getting is a generic .NET exception which occurs when you modify a collection which you are currently traversing.

Regards,
Albert
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Ed
Top achievements
Rank 1
answered on 28 Sep 2008, 09:29 PM
Hello Albert,

Thank you for your reply.  What I am trying to do is to replace the root item of a my RadPanelBar with one of the child items, effectively replacing the tree with a sub-tree.  To illustrate with an example, supposing I have the following structure of items in my PanelBar:

                              A1 (root)
                       |                |
                      B1           B2
                    |       |       |       |
                  C1    C2   C3   C4

I would like to replace it with this structure of panel items:

                   B2 (root)
               |          |
             C3      C4

If I simply try to overwrite the lower-level items in the PanelBar with those further down the tree, I get the error mentioned above.

All advice gratefully received,

Ed

0
Rosi
Telerik team
answered on 29 Sep 2008, 10:39 AM
Hi Ed,

I suspect that you use foreach loop to achieve your task.

Then it is expected to receive the error.

I suggest you replace the foreach loop with for loop.

For Example:
 for (int i = 0; i < RadPanelBar1.Items.Count; i++)  
 {  
    if (RadPanelBar1.Items[i].Level == 1)  
    {  
            RadPanelBar1.Items.Remove(RadPanelBar1.Items[i]);  
    }  

Hope this helps.

Regards,
Rosi
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Ed
Top achievements
Rank 1
answered on 29 Sep 2008, 09:00 PM
Thanks, Rosi -- I'll give that a try.

Cheers,

Ed
0
Ed
Top achievements
Rank 1
answered on 14 Oct 2008, 12:05 AM
Hi Rosi,

Thanks for your suggestion.  Unfortunately, this does not solve my problem.  I need to change the root element of the tree, as described in my post above.  I have tried this code:

    rpbLHNav.DataSource = sdsTree;
    rpbLHNav.DataBind();
    // first remove all unwanted top-level items
    for (int i = rpbLHNav.Items.Count; i > 0; i--)
    {
        RadPanelItem rpi = rpbLHNav.Items[i - 1];
        // we want the new top-level items to be the children of the item with ID newRootID
        if (rpi.Value != newRootID)
        {
            rpbLHNav.Items.Remove(rpi);
        }
    }
    // now copy the old tree to a new tree
    RadPanelBar rpb2 = new RadPanelBar();
    for (int i = rpbLHNav.Items[0].Items.Count; i > 0; i--)
    {
        // adding the children of the item with ID newRootID
        rpb2.Items.Add(rpbLHNav.Items[0].Items[i-1]);
    }
    // now replace the old tree with the new tree
    rpbLHNav = rpb2;
    // this looks as though it should be OK, yet the same old root keeps appearing ...
This looks like it should work, yet the old root element is still shown on the page (I don't know why).

Please help!  This is very frustrating, as I am sure it must be a simple operation yet I cannot work out how to do it.  (If we had access to pointers I would simply swap the pointer to old the root with the pointer to the new element ... !)

Regards,

Ed Graham
0
Accepted
Atanas Korchev
Telerik team
answered on 14 Oct 2008, 07:44 AM
Hi Ed,

Simply changing the reference to the panelbar:
rpbLHNav = rpb2;

won't do nothing. You should remove the old panelbar from the controls collection of its parent control and then add the new panelbar in its place.

Regards,
Albert
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Accepted
Yana
Telerik team
answered on 14 Oct 2008, 10:10 AM
Hi Ed,

Please find attached a sample project demonstrating the needed approach.

Kind regards,
Yana
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Ed
Top achievements
Rank 1
answered on 14 Oct 2008, 04:08 PM
Thanks very much, Yana -- that's just what I needed.
Tags
PanelBar
Asked by
Ed
Top achievements
Rank 1
Answers by
Ed
Top achievements
Rank 1
Atanas Korchev
Telerik team
Rosi
Telerik team
Yana
Telerik team
Share this question
or