C# 遍历TreeView所有节点

public void PrintTreeViewNode(TreeNodeCollection node)
        {
            foreach (TreeNode n in node)
            {
                Response.Write(n.Text+",");
                PrintTreeViewNode(n.ChildNodes);
            }
        }


调用  PrintTreeViewNode(TreeView1.Nodes);
 
//////////////////////////////////////////////////////////////////
private void button1_Click(object sender, EventArgs e)
 

      {

           label1.Text = string.Empty;

           #region 递归
           //1.获取TreeView的所有根节点
           foreach (TreeNode tn in treeView1.Nodes)
           {
               DiGui(tn);
           }

           #endregion

          
       }

       private void DiGui(TreeNode tn)
       {
           //1.将当前节点显示到lable上
           label1.Text += "aaa" +"    " + tn.Text+ "\r\n";
           foreach (TreeNode tnSub in tn.Nodes)
           {
               DiGui(tnSub);

treeView1.SelectedNode = tnSub;    //设置节点
           }
       }

 

你可能感兴趣的:(C# 遍历TreeView所有节点)