1

I'm trying make one treeView with infinite subgroups.

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.

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:

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();
ChrisWue
  • 18,612
  • 4
  • 58
  • 83
santa
  • 147
  • 1
  • 4
  • 16
  • One suggestion: work on more descriptive method names. "`chilnoddoldur`" is not very helpful. – BlueRaja - Danny Pflughoeft Nov 18 '11 at 21:58
  • it is just the name of method to fill childnodes. – santa Nov 18 '11 at 22:00
  • 1
    possible duplicate of [Recursive TreeView in ASP.NET](http://stackoverflow.com/questions/2572721/recursive-treeview-in-asp-net) – Michael Todd Nov 18 '11 at 22:03
  • `code` SqlConnection conn = b.baglan(); if (conn.State == ConnectionState.Open) { conn.Close(); here ı forgot to put this code . yes recursive but ı didnt understand the exact mistake..? } 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();`code` – santa Nov 18 '11 at 22:03
  • @user1054616: Edit your question to add the SQL part, then delete your last comment. – Otiel Nov 18 '11 at 22:08
  • Actually, you didn't make a mistake. I think the issue is that you need to understand how nodes work, a working example of which is provided in the question I suggested. The basic idea is that if you need to add a subnode, you need to add it to the children of the node to which you're trying to add it, not to the node itself. Check out [this question](http://stackoverflow.com/questions/881607/adding-child-nodes-in-treeview) as well. – Michael Todd Nov 18 '11 at 22:10
  • Thank you @MichaelTodd this question is for my other issue may be useful. my problem now ı want to show my groups and subgroups but my code take my groups lıke subgroups:S ı mean: for example ı have marketing and software groups and ı have marketıng 1 and software 1 as subgroups.my code take marketıng and software both group and subgroups. more clear? – santa Nov 18 '11 at 22:21
  • SqlCommand cmd = new SqlCommand("select kgr_sno,kgr_ad,kgr_bsno from kulgrp where kgr_bsno=@id", conn); cmd.Parameters.AddWithValue("@id", kgrSno); my sql string ı couldnt make like code:S ım new here SqlDataAdapter da = new SqlDataAdapter(cmd); – santa Nov 18 '11 at 22:22
  • I think the problem is that you're never making any recursive calls for each group. Inside of your foreach loop you need to somehow add child nodes of that node, and so on, ad infinitum – Adam Rackis Nov 18 '11 at 22:27
  • no ı didnt make sth like this before..How can ı make? – santa Nov 19 '11 at 09:15

2 Answers2

0

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. 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.

Goran
  • 383
  • 4
  • 12
  • ı edited my code like u said but no..the resukt the same..My childnodes take the nodes again – santa Nov 20 '11 at 10:47
  • now ı can see just one subgroup:S ı cant see more..But ı want to see all subroups ı have.. – santa Nov 20 '11 at 13:32
  • The trickiest part is to create logic to filter rows in the datatable to create child datatable for a recursion call. It have to contain all the childnodes + its subchildnodes if there are any - so in the next 'subrecursion' it use same logic to filter subchildnodes etc. It sounds like there is the problem, but It would be easier if you'd show us some code. – Goran Nov 21 '11 at 20:42
0
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

santa
  • 147
  • 1
  • 4
  • 16