Create TreeView control at runtime using C#

In this article we will discuss about how to create treeview at run time in C#. Here is the example that also describe that how we can add nodes to treeview control at run time in C#.

TreeView control

The following function creates a treeview control and add the nodes to it using c#.

    private void CreateTreeViewAtRuntime()
            TreeView TreeView1 = new TreeView();
            TreeView1.Location = new System.Drawing.Point(32, 23);
            TreeView1.Name = "TreeView1";
            TreeView1.Size = new System.Drawing.Size(202, 198);
            //Now we add a node to this treeview so first we create a treenode see this:
            TreeNode Node1 = new TreeNode();
            Node1.Text = "India";
            Node1.Tag = "Country";
            //Add above tree node to treeview
            //Now we create another sub node 
            TreeNode Node_of_Node1 = new TreeNode();
            Node_of_Node1.Text = "New Delhi";
            Node_of_Node1.Tag = "City";
            //Add this node to Node1