当我选择超过4个节点时,它会在树视图中给出错误的结果

时间:2017-09-04 19:18:55

标签: c#

我想像这样制作树:

the tree I want to make

这个包含数据的表格: The table with clearifyed  我试图让这段代码填充父节点和子节点,它给了我第一个节点正确的结果,另一个节点是错误的结果。

这个代码:

//FillTreeView with 5 parent
    public void FillTreeViewWith5(string Query1, string Query2, string Query3, string Query4,
        string Query5,string Query6,string Query7, DataSet Ds, TreeView tr, TreeNode tn1,
        TreeNode tn2, TreeNode tn3, TreeNode tn4, TreeNode tn5,TreeNode tn6,TreeNode tn7)
    {

        int z = 0;
        int r = 0;
        int res;
        //to put it as parameters
        string i1;
        string i2;
        string i3;
        string i4;
        string i5;
        string i6;
        tr.Nodes.Clear();

        SqlDataAdapter da1 = new SqlDataAdapter(Query1,con);
        da1.Fill(Ds, "d1");
        for (int i = 0; i < Ds.Tables["d1"].Rows.Count; i++)
        {
            i1 = Ds.Tables["d1"].Rows[i][0].ToString();
            string x1 = Ds.Tables["d1"].Rows[i][1].ToString();
            tn1 = new TreeNode(x1);
            SqlCommand cmd2 = new SqlCommand();
            cmd2.Connection = con;
            cmd2.CommandText = Query2;
            cmd2.Parameters.AddWithValue("@i1", i1);
            SqlDataAdapter da2 = new SqlDataAdapter(cmd2);
            da2.Fill(Ds, "d2");
            for (int j = 0; j < Ds.Tables["d2"].Rows.Count; j++)
            {
                i2= Ds.Tables["d2"].Rows[j][0].ToString();
                string x2 = Ds.Tables["d2"].Rows[j][1].ToString();
                tn2 = new TreeNode(x2);
                tn1.Nodes.Add(tn2);
                SqlCommand cmd3 = new SqlCommand();
                cmd3.Connection = con;
                cmd3.CommandText = Query3;
                cmd3.Parameters.AddWithValue("@i2", i2);
                SqlDataAdapter da3 = new SqlDataAdapter(cmd3);
                da3.Fill(Ds, "d3");
                for (int b = 0; b < Ds.Tables["d3"].Rows.Count; b++)
                {
                    i3 = Ds.Tables["d3"].Rows[b][0].ToString();
                    string x3 = Ds.Tables["d3"].Rows[b][1].ToString();
                    tn3 = new TreeNode(x3);
                    tn2.Nodes.Add(tn3);
                    SqlCommand cmd4 = new SqlCommand();
                    cmd4.Connection = con;
                    cmd4.CommandText = Query4;
                    cmd4.Parameters.AddWithValue("@i3", i3);
                    SqlDataAdapter da4 = new SqlDataAdapter(cmd4);
                    da4.Fill(Ds, "d4");
                    for (int c = 0; c < Ds.Tables["d4"].Rows.Count; c++)
                    {
                        i4 = Ds.Tables["d4"].Rows[c][0].ToString();
                        string x4 = Ds.Tables["d4"].Rows[c][1].ToString();
                        tn4 = new TreeNode(x4);
                        tn3.Nodes.Add(tn4);
                        SqlCommand cmd5 = new SqlCommand();
                        cmd5.Connection = con;
                        cmd5.CommandText = Query5;
                        cmd5.Parameters.AddWithValue("@i4", i4);
                        SqlDataAdapter da5 = new SqlDataAdapter(cmd5);
                        da5.Fill(Ds, "d5");

                        for ( int m= 0; m <Ds.Tables["d5"].Rows.Count ; m++)
                        {

                            i5 = Ds.Tables["d5"].Rows[m][0].ToString();
                           string x5= Ds.Tables["d5"].Rows[m][1].ToString();
                           tn5 = new TreeNode(x5);
                           tn4.Nodes.Add(tn5);
                           SqlCommand cmd6 = new SqlCommand();
                           cmd6.CommandText = Query6;
                           cmd6.Connection = con;
                           cmd6.Parameters.AddWithValue( "@i5",i5);
                           SqlDataAdapter da6 = new SqlDataAdapter(cmd6);
                           da6.Fill(Ds, "d6");

                            for (int p= 0; p < Ds.Tables["d6"].Rows.Count; p++)
                            {
                                i6 = Ds.Tables["d6"].Rows[p][0].ToString();
                                string x6 = Ds.Tables["d6"].Rows[p][1].ToString();
                                tn6 = new TreeNode(x6);
                                tn5.Nodes.Add(tn6);
                                SqlCommand cmd7 = new SqlCommand();
                                cmd7.CommandText = Query7;
                                cmd7.Connection = con;
                                cmd7.Parameters.AddWithValue("@i6", i6);
                                SqlDataAdapter da7 = new SqlDataAdapter(cmd7);
                                da7.Fill(Ds, "d7");
                                res=Ds.Tables["d7"].Rows.Count;
                                for (r = z; r < res; r++)
                                {
                                    string i7 = Ds.Tables["d7"].Rows[r][0].ToString();
                                    string x7 = Ds.Tables["d7"].Rows[r][1].ToString();
                                    tn7 = new TreeNode(x7);
                                    tn6.Nodes.Add(tn7);
                                }
                                z = r;
                                res = res + res;

                            }






                        }


                    }

                }


            }


        }

        tr.Nodes.Add(tn1);
        tr.ExpandAll();

    }

调用方法:

   public void Fill(TreeView tr)
   {
       TreeNode tn1=new TreeNode();
       TreeNode tn2=new TreeNode();
       TreeNode tn3 = new TreeNode();
       TreeNode tn4 = new TreeNode();
       TreeNode tn5 = new TreeNode();
       TreeNode tn6 = new TreeNode();
       TreeNode tn7 = new TreeNode();
       DataSet ds = new DataSet();
       A.FillTreeViewWith5("select Re_id,Re_nm from RegionsTbl where Parent_id=0", "select Re_id,Re_nm from RegionsTbl where Parent_id=@i1", "select Re_id,Re_nm from RegionsTbl where Parent_id=@i2", "select Re_id,Re_nm from RegionsTbl where Parent_id=@i3", "select Re_id,Re_nm from RegionsTbl where Parent_id=@i4", "select Re_id,Re_nm from RegionsTbl where Parent_id=@i5", "select Re_id,Re_nm from RegionsTbl where Parent_id=@i6", ds, tr, tn1, tn2, tn3, tn4, tn5, tn6,tn7);


   }

它给了我第一个节点正确而另一个没有。

这段代码有什么问题?如果有更好的方式会很好。

1 个答案:

答案 0 :(得分:0)

很抱歉,但这段代码在很多层面都很糟糕。它难以阅读,难以维护,主要方法是长期,不灵活(如果你需要8,9或10级?),打开的连接太多等等。< / p>

要修复它,您只需要使用递归。这意味着一个代码块在循环中执行多次。这种方式代码更简单,只有一个查询等。

所以,解释它是如何完成的:

首先,从表中获取所有区域,不进行任何过滤。只需将整个表从服务器加载到DataTable即可。然后,我们将递归地使用AddTreeNode方法(对该方法的一次调用,该方法多次调用自己)。

AddTreeNode有三个参数:parentNode(添加了其他节点的节点),table(所有带有区域的行的DataTable,在开头提取)和parentId(父ID)。

当我们第一次致电AddTreeNode时,我们会传递null(作为父级,因为没有父级,TreeView将被使用,DataTable和0作为{{1 (第一级)。然后过滤parentId以仅获取已传递DataTable的行,其中每一行都以parentId添加到TreeNode(到{{1}在此之后,parentNode调用自身,这次将新创建的节点作为TreeView传递,将AddTreeNode和新创建的节点ID作为{{1}传递无论需要多少级别,都会在有节点指定父节点的情况下获取。parentNode

现在,代码:

将其放在代码中的某个位置,例如DataTable

parentId

AddTreeNode方法:

Form_Load

为了测试它,我使用了你的一些数据,而不是//first, fetch data SqlConnection con = new SqlConnection("your connection string"); //get all the data string query = "select Re_id,Re_nm, parent_id from RegionsTbl"; SqlDataAdapter da = new SqlDataAdapter(query, con); DataTable table = new DataTable(); da.Fill(table); //call AddTreeNode method AddTreeNode(null, table, 0); tr.ExpandAll();

AddTreeNode

尝试通过这个例子并理解它。如果您还有其他问题,请随时提出。

相关问题