龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > asp.net编程 >

递归实现TreeView数据绑定代码(2)

时间:2009-12-21 11:47来源:未知 作者:admin 点击:
分享到:
1 if (!IsPostBack) 2 { 3 bindtree("0"); //传入第0个pid开始遍历根节点 4 } 5 6 private DataSet Getdata(string pid) //在这里我们传入一个pid 7 { 8 SqlConnection con = new SqlConnection

1    if (!IsPostBack)
 2         {
 3             bindtree("0"); //传入第0个pid开始遍历根节点
 4         }
 5     
 6    private DataSet Getdata(string pid)    //在这里我们传入一个pid
 7     {
 8         SqlConnection con = new SqlConnection(@"数据库");
 9         SqlCommand com = new SqlCommand(" 数据库查找 where pid=" + pid, con);
10         SqlDataAdapter da = new SqlDataAdapter(com);
11         DataSet ds = new DataSet();
12         da.Fill(ds);
13         return ds;              //返回含有pid的数据
14     }
15     private void bindtree(string pid)
16     {
17         DataSet ds = Getdata(pid);
18         if (ds.Tables[0].Rows.Count > 0)
19         {
20             for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
21             {
22                 TreeNode node = new TreeNode(ds.Tables[0].Rows[i]["字段"].ToString(), ds.Tables[0].Rows[i]["id"].ToString());   //这是在找数据库中的节点
23                 this.TreeView1.Nodes.Add(node);    //把这个节点添加到控件中
24                 bindnode(node);
25
26             }
27         }
28     }
29     private void bindnode(TreeNode nd)  //就是一个递归的开始 遍历根节点下面的子节点
30     {
31         DataSet ds = Getdata(nd.Value);
32         for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
33         {
34             TreeNode node = new TreeNode();
35             node.Text = ds.Tables[0].Rows[i]["name"].ToString();
36             node.Value = ds.Tables[0].Rows[i]["id"].ToString();
37
38             nd.ChildNodes.Add(node);
39
40             bindnode(node);
41         }
42     }


精彩图集

赞助商链接