TreeView控件递归绑定数据学习

#region 绑定父节点(第一级)
    private void BindTree(string pid)
    {
        DataSet ds = GetData(pid);
        if (ds.Tables[0].Rows.Count > 0)
        {
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                TreeNode node = new TreeNode();
                node.Text = ds.Tables[0].Rows[i]["Name"].ToString();
                node.Value = ds.Tables[0].Rows[i]["ID"].ToString();
                this.TreeView1.Nodes.Add(node);
                BindNode(node);
            }
        }
    }
    #endregion

#region 绑定子节点
    private void BindNode(TreeNode nd)
    {
DataSet ds = GetData(nd.Value);
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            TreeNode node = new TreeNode();
            node.Text = ds.Tables[0].Rows[i]["Name"].ToString();
            node.Value = ds.Tables[0].Rows[i]["ID"].ToString();

            nd.ChildNodes.Add(node);

            BindNode(node);
        }
    }
    #endregion

你可能感兴趣的:(TreeView控件递归绑定数据学习)