如何在asp.net的treeview中显示MLM数据

时间:2019-06-10 08:36:20

标签: c# asp.net treeview mlm

MLM data sample

我有多级营销软件数据,其中一个人可以加入很多其他人,再次加入的人也可以这样做。

我已经尝试过这些代码

     protected void load_data()
         {

        strQuery = "select * from redjoining where id=995";
        cmd = new SqlCommand(strQuery);
        dt = dbcon.GetData(cmd);

        string cat_code = dt.Rows[0]["id"].ToString();
        string cat_name = dt.Rows[0]["userName"].ToString();

        TreeNode parent = new TreeNode();
        parent.Value = cat_code;
        parent.Text = cat_name;
        TreeView1.Nodes.Add(parent);
        add_childs(parent, cat_code);

    }

现在要添加子节点,我使用此代码,但它仅添加仅顶部的单个记录。

  protected void add_childs(TreeNode tn, string category_code) 
  {

        strQuery = "select * from redjoining where sponserid=@category_code";
        cmd = new SqlCommand(strQuery);
        cmd.Parameters.AddWithValue("@category_code", category_code);
        dt = dbcon.GetData(cmd);

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            string cat_code = dt.Rows[0]["id"].ToString();
            string cat_name = dt.Rows[0]["userName"].ToString();

            TreeNode child = new TreeNode();
            child.Value = cat_code;
            child.Text = cat_name;
            tn.ChildNodes.Add(child);

            add_childs(child, cat_code); //calling the same function
        }
    }

1 个答案:

答案 0 :(得分:0)

在树状视图中添加子记录的方法存在问题,请检查以下代码

protected void add_childs(TreeNode tn, string category_code) 
  {

        strQuery = "select * from redjoining where sponserid=@category_code";
        cmd = new SqlCommand(strQuery);
        cmd.Parameters.AddWithValue("@category_code", category_code);
        dt = dbcon.GetData(cmd);

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            string cat_code = dt.Rows[i]["id"].ToString();
            string cat_name = dt.Rows[i]["userName"].ToString();

            TreeNode child = new TreeNode();
            child.Value = cat_code;
            child.Text = cat_name;
            tn.ChildNodes.Add(child);

            add_childs(child, cat_code); //calling the same function
        }
    }

在您的代码中,您始终使用dt.Rows[0]["id"].ToString()

使用i作为您的dt有多个记录,并且您正在循环。

希望这会有所帮助。