Thursday, 15 May 2014

ASP.NET - How to cretae TreeView programmatically

- Create TreeView programmatically, add nodes, etc

Watch this example on YouTube



            TreeView tv = new TreeView();
            tv.ID = "treeID";
            tv.ExpandDepth = 1;
            tv.ExpandAll();

            TreeNode tn = new TreeNode();
            tn.Text = "Node";
            tn.Value = "n1";
            tn.Expanded = true;

            TreeNode tnChild1 = new TreeNode();
            tnChild1.Text = "Child";
            tn.ChildNodes.Add(tnChild1);

            TreeNode tnChild2 = new TreeNode();
            tnChild2.Text = "Child2";
            tn.ChildNodes.Add(tnChild2);

            TreeNode tnGrandChild = new TreeNode();
            tnGrandChild.Text = "GrandChild";
            tnChild2.ChildNodes.Add(tnGrandChild);

            tv.Nodes.Add(tn);
            form1.Controls.Add(tv);

No comments:

Post a Comment