简体   繁体   English

树视图无限循环

[英]treeview infinite loop

I'm trying make one treeView with infinite subgroups. 我正在尝试使用无限子组制作一个treeView

I can add my groups but I couldn't add my subgroups. 我可以添加组,但不能添加子组。 For subgroup the output shows my group value. 对于子组,输出显示我的组值。 My code for subgroup is below: I think there is something wrong with my SQL string but I don't know what is. 我的分组代码如下:我认为我的SQL字符串有问题,但我不知道这是什么。

private void chilnoddoldur(DataTable dt, TreeNodeCollection treeNodeCollection)
//fill childnodes
{
    foreach (DataRow dr in dt.Rows)
    {
        TreeNode child = new TreeNode();
        child.Text = dr["kgr_ad"].ToString();
        child.Value = dr["kgr_bsno"].ToString();
        if (child.ChildNodes.Count > 0)
        {
            child.PopulateOnDemand = true;
        }
        child.SelectAction = TreeNodeSelectAction.SelectExpand;
        child.Expand();
        child.Selected = true;
        treeNodeCollection.Add(child);
   }
}

Here is the SQL Code: 这是SQL代码:

SqlConnection conn = b.baglan();
if (conn.State == ConnectionState.Open) 
{ 
    conn.Close();
} 
conn.Open(); 
SqlCommand cmd = new SqlCommand("select kgr_sno,kgr_ad,kgr_bsno from kulgrp where kgr_bsno=@id", conn); 
cmd.Parameters.AddWithValue("@id", kgrSno); 
SqlDataAdapter da = new SqlDataAdapter(cmd); 
DataTable dt = new DataTable(); 
da.Fill(dt); 
kgrBsno.ChildNodes.Clear(); 
chilnoddoldur(dt, kgrBsno.ChildNodes); 
conn.Close();

If some node has child nodes, you should create recursion - call chilnoddoldur method again, but it's important to pass appropriate argments: datatable should have only rows with child elements, and treeNodeCollection should represent childNodeCollection of the current node. 如果某些节点具有子节点,则应创建递归-再次调用chilnoddoldur方法,但是传递适当的参数很重要:datatable应该只包含带有子元素的行,而treeNodeCollection应该代表当前节点的childNodeCollection。 It should look something like this: 它看起来应该像这样:

if (child.ChildNodes.Count > 0)
{
    child.PopulateOnDemand = true;
    //recursion call - childDt is datatable with childnode rows
    chilnoddoldur(childDt, child.ChildNodes)
}

If some of this childnodes has its own childnodes, it will make another recursion call, if not, when method runs out, it will continue running its parent method, etc. 如果某些子节点具有自己的子节点,它将进行另一个递归调用,如果没有,当方法用完时,它将继续运行其父方法,依此类推。

I hope I was clear enough to give you a basic idea. 我希望我足够清楚,可以给您一个基本的想法。

foreach (DataRow dr in dt.Rows)
    {
        TreeNode child = new TreeNode();
        child.Text = dr["kgr_ad"].ToString();
        child.Value = dr["kgr_sno"].ToString();

           child.CollapseAll();

            kgrBsno.ChildNodes.Add(child);
            AltGruplariYaz(child, Convert.ToInt32(dr["kgr_sno"]));
}

the answer of my question 我的问题的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM