locked
How to merge two treeView into one?? RRS feed

  • Question

  • I am making a Windows application using .NET 2005 (C#).

     

    I need to merge 2 treeView control objects into one treeView.

     

    Any sample source code or algorithm?

     

    Please help and I will reward you.

    Tuesday, June 12, 2007 6:57 AM

Answers

  • It's based on what kind of  *merge* do you want, I assume you want to merge two TreeViews into one as insert a whole tree into the root of the another, below is a sample I write for your information, hope it helps;

    Code Snippet

    private void MergeTree_Load(object sender, EventArgs e)

            {

                TreeNode root1 = new TreeNode("Root1");

                root1.Nodes.Add("aaa1");

                root1.Nodes.Add("bbb1");

                root1.Nodes.Add("ccc1");

                this.treeView1.Nodes.Add(root1);

     

                TreeNode root2 = new TreeNode("Root2");

                root2.Nodes.Add("aaa2");

                TreeNode node = new TreeNode("child");

                node.Nodes.Add("child1");

                node.Nodes.Add("child2");

                root2.Nodes.Add(node);

                root2.Nodes.Add("bbb2");

                root2.Nodes.Add("ccc2");

                this.treeView2.Nodes.Add(root2);

            }

     

            private void button1_Click(object sender, EventArgs e)

            {

                TreeNode nodes = this.treeView2.Nodes[0].Clone() as TreeNode;

                this.treeView1.Nodes.Add(nodes);

            }

     

    Monday, June 18, 2007 10:27 AM