使用递归的方法生产TreeView

    public void ParentNode()    //创建父节点
    {
        string sql;
        if (p == "999999")
        {
            sql = "select id,LBMC from WJLBB where parent_id=0";
        }
        else
        {
            sql = "select id,LBMC from WJLBB where parent_id=0 and bmbh='" + bmbh + "'";
        }
        DataSet ds = newdbclient.DataSets(sql);
        if (ds.Tables[0].Rows.Count != 0)
        {
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                TreeNode tn = new TreeNode();
                tn.Text = ds.Tables[0].Rows[i]["LBMC"].ToString();
                tn.Value = ds.Tables[0].Rows[i]["id"].ToString();
                TreeView1.Nodes.Add(tn);
                ChildNode(tn, tn.Value);
                //this.treeView1.Nodes.Add("onclick", "CheckEvent()");
            }
        }

    }

    public void ChildNode(TreeNode node, string fdeptid)    //递归取出父节点下的子节点
    {
        string sql = "select id,LBMC from WJLBB where kf='开放' and parent_id='" + fdeptid + "'";
        DataSet ds = newdbclient.DataSets(sql);
        if (ds.Tables[0].Rows.Count != 0)
        {
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                TreeNode tn = new TreeNode();
                tn.Text = ds.Tables[0].Rows[i]["LBMC"].ToString();
                tn.Value = ds.Tables[0].Rows[i]["id"].ToString();
                node.ChildNodes.Add(tn);
                //UserNode(tn, tn.Value);     //在子节点下创建人员节点
                ChildNode(tn, tn.Value);
            }
        }
    }

转载于:https://www.cnblogs.com/liulanglang/archive/2012/01/04/2311876.html

你可能感兴趣的:(使用递归的方法生产TreeView)