how to add nodes to treeview in C#.net at run time

Last post 07-03-2009 6:19 AM by SSA. 1 replies.

Sort Posts:

  • how to add nodes to treeview in C#.net at run time

    07-02-2009, 4:39 PM
    • Member
      4 point Member
    • nishantdhawan
    • Member since 06-16-2009, 10:24 PM
    • San Jose
    • Posts 3

    I know there are three ways to populate a treeview:

    1. through Sitemap

    2. thorugh XML file

    3. Programmatically

    So my requirement is to add a node in the Treeview control with user entering the name of the node and the link for that node.

    Also, there should be an option like whether user wants to enter a new node or enter a child node under existing node.

    There are only 2 levels.

    Ex.

    1

        1.1

        1.2

    2

        2.1

        2.2

    So now I should have the option in a Dropdown that whether I want to add a new node (for ex. 3) or I want to add a child node under 1 or 2.

    If I add a new node, then it should automatically come into the Dropdown control.

    Please help me. Thanks in advance.

  • Re: how to add nodes to treeview in C#.net at run time

    07-03-2009, 6:19 AM
    Answer
    • Contributor
      2,490 point Contributor
    • SSA
    • Member since 05-07-2009, 3:16 PM
    • Amsterdam, The Nederlands
    • Posts 409

    If you want to add root node (parent node) then you can do it like this:

    TreeNode node = CreateParentNode(NameOfnode, URL);

    Your defination for CreateParentNode could be like this:

    private TreeNode CreateMenuItem(string strText,int count, string strUrl)
        {
            TreeNode Node = new TreeNode();
            try
            {
                ////Create new menu item
                
                ////Set properties of the menu item, -1 for parent menu items
                if (count != -1)
                {
                    if (count != 0)
                    {
                        Node.Text = strText + "[" + count + "]";
                        Node.NavigateUrl = strUrl;
                    }
                    else
                    {
                        Node.Text = strText;
                        //Node.NavigateUrl = strUrl;
                        
                    }
                }
                else
                {
                    Node.Text = strText;
                    Node.NavigateUrl = strUrl;
                }
                
                return Node;
            }

    private TreeNode CreateMenuItem(string strText, string strUrl)

        {

            TreeNode Node = new TreeNode();

            try

            {

                   

                        Node.Text = strText ;

                        Node.NavigateUrl = strUrl;

        return Node;

            }


    Now node returned by this function can be added to your TreeView,

    YourTreeView.Nodes.Add(node);

    You also want to add child node under a parent, you need to have parent node:

    You can retrive the parent node by index 

    TreeNode parent = TreeView.Nodes[index];

    write a method to add childs:

    private void AddChildMenuItems(string Text, string URL, TreeNode ParentNode)

    {

    TreeNode ChildNode = new TreeNode();

    ChildNode = CreateParentNode(NameOfnode, URL); // First created

    ParentNode.ChildNode.Add(ChildNode );

    }


    You can pass, input from dropdownlist. Once Item is added rebind your dropdown list.

Page 1 of 1 (2 items)