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

CloneTreeView

4 Answers 116 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Sanjay
Top achievements
Rank 1
Sanjay asked on 29 Jun 2015, 05:45 PM

Hi

 

Please advise how i can clone a tree view

 

I have a radDropDownList and a RadTreeView.

upon selection in the drop down, my data chages, though the table structure remain the same. Suppose i have options 1 and 2 in the drop down. user selects option 1 and tree is populated. User makes some selections in tree check boxes, then select option 2 from drop down list, new data populateed in tree view, and user does further selections in tree view. Now user goes back to option 1, i should get the tree view with data and tick selections

 below are my codes, but this does not seem to be good. tvNode is in designer

local variables

        RadTreeView tvAsset = new RadTreeView();
        RadTreeView tvDriver = new RadTreeView();

        private void cbStruc_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {
            if (cbStruc.SelectedItem == null)
                return;

            int i = 0;
            Image imagetemp;
            switch (cbStruc.SelectedItem.ToString())
            {
                case "Assets":
                    if (tvAsset.Nodes.Count == 0)
                    {
                        TreeView tv = new TreeView();
                        new GroupMatrix().PopulateTreeCategories(tv, Globals.uLogin.lMatrix, cbStruc.SelectedItem.ToString());
                        tvNode.ClearSelection();
                        tvNode.DataSource = Globals.dtGMTreeCategory;

                        tvAsset = CloneTreeNodes(tvNode);
                    }
                    else
                    {
                        tvNode.ClearSelection();
                        tvNode = CloneTreeNodes(tvAsset);
                    }
                    break;

                case "Drivers":
                    if (tvDriver.Nodes.Count == 0)
                    {
                        TreeView tv = new TreeView();
                        new GroupMatrix().PopulateTreeCategories(tv, Globals.uLogin.lMatrix, cbStruc.SelectedItem.ToString());
                        tvNode.ClearSelection();
                        tvNode.DataSource = Globals.dtGMTreeCategory;

                        tvDriver = CloneTreeNodes(tvNode);
                    }
                    else
                        tvNode = CloneTreeNodes(tvDriver);
                    break;

            }
            tvNode.ExpandAll();
        }

    private void cbStruc_SelectedIndexChanging(object sender, Telerik.WinControls.UI.Data.PositionChangingCancelEventArgs e)
        {
            if (cbStruc.SelectedItem != null)
                switch (cbStruc.SelectedItem.ToString())
                {
                    case "Assets":
                        tvAsset = CloneTreeNodes(tvNode);
                        break;

                    case "Drivers":
                        tvDriver = CloneTreeNodes(tvNode);
                        break;

                }
        }

        RadTreeView CloneTreeNodes(RadTreeView tvFrom)
        {
            RadTreeView treeview2 = new RadTreeView();
            if (tvFrom.Nodes.Count == 0)
                return treeview2;

            foreach (RadTreeNode tn in tvFrom.Nodes)
                treeview2.Nodes.Add((RadTreeNode)tn.Clone());
            
            treeview2.DataSource = tvFrom.DataSource;
            return treeview2;
        }


Kindly advise

 

Thank you

4 Answers, 1 is accepted

Sort by
0
Sanjay
Top achievements
Rank 1
answered on 29 Jun 2015, 06:32 PM

Hello again

 I missed the following

            this.tvNode.DisplayMember = "GMDescription";
            this.tvNode.ParentMember = "ParentGMID";
            this.tvNode.ChildMember = "GMID";
            this.tvNode.ValueMember = "StrucID";

now, in the checked nodes, how do i get the above value. they are all populated in the datasource.

foreach (RadTreeNode rtn in myTV.CheckedNodes)
            {
                rtn.??? does not have the relevant value, except for rtn.Value and rtn.Text
            }

 Thank you

0
Hristo
Telerik team
answered on 02 Jul 2015, 01:41 PM
Hi Sanjay,

Thank you for writing.

If I understand correctly by the value above you mean the value of the node in the tree which is one level higher than the current checked tree node. If this is the case you could directly access the parent, please see my code snippet below: 
private void GetParentOfCheckedNode(RadTreeView radTreeView)
{
    foreach (RadTreeNode node in this.radTreeView1.CheckedNodes)
    {
        if (node.Parent != null)
        {
            var value = node.Parent.Value;
            var dataBoundItem = node.Parent.DataBoundItem;
        }
    }
}

If you are still experiencing issues or I misunderstood you, please get back to me with additional information of what you are trying to accomplish.

I hope this helps. Should you have further questions please do not hesitate to write back.

Regards,
Hristo Merdjanov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Sanjay
Top achievements
Rank 1
answered on 02 Jul 2015, 06:56 PM

Hello. Thank you for replying

I mentioned 2 issues above. let us address it 1 by 1.

I attached a sample of my codes

I have a radDropDownList and a RadTreeView. Upon radDropDownList change index, data in tree changes. On radDropDownList Asset selection, user checks "Naveo". Then user changes to radDropDownList Driver selection and selects "All"

Now, if the user changes back to radDropDownList  Asset selection, the tree should show the Asset Data with the checked nodes ("Naveo)

 Finally, when show button is clicked, i should get StrucID for checked Assets and checked Driver

Please rename to .zip. Also note that my subscription just expired and necessary is being done to renew

Kindly advise 

Thank you

 

0
Hristo
Telerik team
answered on 07 Jul 2015, 02:40 PM
Hi Sanjay,

Thank you for writing back.

In your project, you are at one time manipulating the data source of the tree and at another removing RadTreeNodes. It is better that you choose one approach and stick with it. Considering you scenario, I believe that working only with the DataSource property should be enough.

I created an example project with a sample implementation. Basically, you would need to store the checked nodes when changing the data source of RadTreeView. Besides the project, I am also sending you a gif file showing the result on my end.

I hope this helps. Should you have further questions please do not hesitate to write back.

Regards,
Hristo Merdjanov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Treeview
Asked by
Sanjay
Top achievements
Rank 1
Answers by
Sanjay
Top achievements
Rank 1
Hristo
Telerik team
Share this question
or