如何在树视图中获取所有展开的节点? [英] How can I get all expanded nodes in treeview?

查看:47
本文介绍了如何在树视图中获取所有展开的节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 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.

当用户将数据添加到数据库时,它必须自动添加到 TreeView.我可以通过清除所有节点、添加默认节点并将包括新节点在内的所有数据添加到我的 TreeView 来成功完成此操作,但新 TreeView 的所有节点都已折叠.

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.

推荐答案

据我所知,您想在刷新树视图(通过添加新数据甚至删除一些数据)后保存树视图地图,然后展开所有展开默认情况下,另一个节点是折叠的.解决办法是:

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) 在刷新前保存展开的树视图节点

1) save expanded tree view nodes before refresh

2) 刷新树视图数据(请注意,如果您要删除节点,请将其也从保存的列表中删除)

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

3) 设置之前保存的树状视图

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;
        }

现在您已经在列表中收集了扩展节点名称,并且想要恢复树视图外观,您需要 2 个函数,一个是按名称检索节点的函数,另一个是扩展所选节点及其父节点的函数,代码如下:

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 
            }

        }

下面展示了函数的用法

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);
                }

            }

        }

这篇关于如何在树视图中获取所有展开的节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆