日期:2014-05-17  浏览次数:20767 次

怎么用递归绑定数据
现在又一个表,有三列,一列id,一列名字,一列是上一级id(上一级的目录的id),怎么用递归把它绑定在treeview上

------解决方案--------------------
C# code
private void Form1_Load(object sender, EventArgs e)
        {
            string sql = string.Format("select * from flb");
            DataTable dt = SX.mydb.GET_DataTable(sql);
            treeadd(dt,0,(TreeNode)null);
            treeView1.ExpandAll();
        }
           


         /// <summary>
        /// 根据菜单表把权限树加载好
        /// </summary>
        /// <param name="dt">数据源</param>
        /// <param name="Filterid">上级ID</param>
        /// <param name="node">树节点</param>
        private void treeadd(DataTable dt, int Filterid, TreeNode node)
        {
            DataView subView = dt.DefaultView;     
                 subView.RowFilter = "lslb = " + Filterid; //获得ID级别为当前ID的
                  foreach (DataRowView item in subView)
                    {
                      //如果上级ID为0就直接加载树下面
                        if (Filterid == 0)
                        {
                            TreeNode treenodel = new TreeNode();  //创建一个节点
                            treenodel.Text = item["flmc"].ToString(); //节点名称
                            int id =Convert.ToInt32(item["ID"]);    
                            treenodel.Tag = id.ToString();     //节点值
                            treeView1.Nodes.Add(treenodel);  // 如果级别ID为0就直接加到根节点
                            treeadd(dt, id, treenodel); // 递归调用
                        }
                        else
                        {      
                            //不为0就加载传进来的子节点下
                            TreeNode treenodel = new TreeNode();
                            treenodel.Text = item["flmc"].ToString();
                            int id = Convert.ToInt32(item["ID"]);
                            treenodel.Tag = id.ToString();
                            node.Nodes.Add(treenodel);  //把节点加入到当前节点下,
                            treeadd(dt, id, treenodel); 
                        }
                   }