展开和折叠树节点

折叠树节点第一层

 if (treeView1.Nodes.Count > 0)
                treeView1.Nodes[0].Expand();

        /// 
        /// 节点的展开和折叠
        /// 
        /// 
        /// 
        private void tvDocInfo_AfterExpand(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Parent == null)
            {
                TreeView tree = (TreeView)sender;
                tree.AfterCheck -= new TreeViewEventHandler(this.tvDocInfo_AfterExpand);
                //折叠其他同级节点   
                TreeNode parentNode = e.Node.Parent;
                if (parentNode != null)
                {
                    foreach (TreeNode tn in parentNode.Nodes)
                    {
                        if (tn != e.Node)
                            tn.Collapse();
                    }
                }
                else
                {
                    foreach (TreeNode tn in tree.Nodes)
                    {
                        if (tn != e.Node)
                            tn.Collapse();
                    }
                }
            }

        }

 

你可能感兴趣的:(C#)