Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get all expanded nodes in treeview?

Tags:

c#

nodes

treeview

I have a program that contains a TreeView. All of my nodes except for the root and two nodes under the root are loaded from a database.

When the user adds data to the database it must be automatically added to the TreeView. I can successfully do this by clearing all the nodes, add the default nodes and add all the data including the new one to my TreeView, but all nodes of the new TreeView are collapsed.

Our client wants to retain all expanded nodes but still add the new data he just added. Is there any way to know all expanded nodes and expand it again once collapsed or refreshed? Thank you for any response.

like image 284
Brenelyn Avatar asked Jan 30 '13 04:01

Brenelyn


1 Answers

Hi As i understand you want to save you tree view map after you refresh tree view (by adding new data or even remove some) you want to expand all expanded nodes the other are collapsed by default. Solution is:

1) save expanded tree view nodes before refresh

2) refresh tree view data (notice that if you are removing a node , remove it from saved list as well)

3) set tree view map which saved before

saving tree view map (only expanded nodes)-> This Code Look through a tree view node collection and saves expanded nodes Names in a string list

List<string> collectExpandedNodes(TreeNodeCollection Nodes)
        {
            List<string> _lst = new List<string>();
            foreach (TreeNode checknode in Nodes)
            {
                if (checknode.IsExpanded)
                    _lst.Add(checknode.Name);
                if (checknode.Nodes.Count > 0)
                    _lst.AddRange(collectExpandedNodes(checknode.Nodes));
            }
            return _lst;
        }

Now you have collected expanded nodes name in a list and you want to gain back your tree view appearance you need 2 functions a function which retrieves node by a name and a function which expand selected node and it's parents the following codes does :

This function retrieves a pointer to selected node Name if node is exist in Tree Node Collection

TreeNode FindNodeByName(TreeNodeCollection NodesCollection , string Name)
        {
          TreeNode returnNode = null; // Default value to return
          foreach (TreeNode checkNode in NodesCollection)
            {
                if (checkNode.Name == Name)  //checks if this node name is correct
                    returnNode = checkNode;
                else if (checkNode.Nodes.Count > 0 ) //node has child
                {
                    returnNode = FindNodeByName(checkNode.Nodes , Name);
                }

              if (returnNode != null) //check if founded do not continue and break
              {
                  return returnNode;
              }

            }
            //not found
            return returnNode;
        }

and This Function Expand node and it's parents

void expandNodePath(TreeNode node)
        {
            if (node == null)
                return;
            if (node.Level != 0) //check if it is not root
            {
                node.Expand();
                expandNodePath(node.Parent);
            }
            else
            {
                node.Expand(); // this is root 
            }

        }

and the following show you the usage of the functions

private void button4_Click(object sender, EventArgs e)
        {
            //saving expanded nodes
            List<string> ExpandedNodes = new List<string>();
            ExpandedNodes = collectExpandedNodes(treeView1.Nodes);
            //resetting tree view nodes status to colapsed
            treeView1.CollapseAll();

            //Restore it back
            if (ExpandedNodes.Count > 0)
            {
                TreeNode IamExpandedNode;
                for (int i = 0; i < ExpandedNodes.Count;i++ )
                {
                    IamExpandedNode = FindNodeByName(treeView1.Nodes, ExpandedNodes[i]);
                    expandNodePath(IamExpandedNode);
                }

            }

        }
like image 170
saeed Avatar answered Sep 26 '22 19:09

saeed