Tuesday, 31 March 2015

ASP.NET - Create TreeView Programmatically

Watch this example on YouTube:

            TreeNode tNode1 = new TreeNode();
            tNode1.Text = "HeadNode1";
            tNode1.Value = "HeadNode1";

            tNode1.CollapseAll();

            TreeNode h1ChildNode1 = new TreeNode();
            h1ChildNode1.Text = "Head1Child1";
            tNode1.ChildNodes.Add(h1ChildNode1);

            TreeNode h1GrandChild1 = new TreeNode();
            h1GrandChild1.Text = "Head1Child1Grand1";
            h1ChildNode1.ChildNodes.Add(h1GrandChild1);

            TreeNode h1ChildNode2 = new TreeNode();
            h1ChildNode2.Text = "Head1Child2";
            tNode1.ChildNodes.Add(h1ChildNode2);

            TreeNode h1ChildNode3 = new TreeNode();
            h1ChildNode3.Text = "Head1Child3";
            tNode1.ChildNodes.Add(h1ChildNode3);

 


         TreeNode tNode2 = new TreeNode();
            tNode2.Text = "HeadNode2";
            tNode2.Value = "HeadNode2";
            tNode2.CollapseAll();

            TreeNode h2ChildNode1 = new TreeNode();
            h2ChildNode1.Text = "Head2Child1";
            tNode2.ChildNodes.Add(h2ChildNode1);

            TreeNode h2ChildNode2 = new TreeNode();
            h2ChildNode2.Text = "Head2Child2";
            tNode2.ChildNodes.Add(h2ChildNode2);

            TreeNode h2ChildNode3 = new TreeNode();
            h2ChildNode3.Text = "Head2Child3";
            tNode2.ChildNodes.Add(h2ChildNode3);

            TreeView myTree = new TreeView();
            myTree.CheckedNodes.Add(tNode1);
            myTree.Nodes.Add(tNode1);
            myTree.Nodes.Add(tNode2);
            Panel1.Controls.Add(myTree);

No comments:

Post a Comment