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

 
 
 
posted @ 2013-02-20 14:53  金虹巴巴  阅读(33793)  评论(0编辑  收藏  举报