Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hey guys,

I have a problem which I cant solve yet. I just need a hint, how to solve it.

Problem:
I get the following list from a sql query:

GROUP:
01. Employees/Administration
01. Employees/Managment
01. Employees/Sales
...
02. Type/Member
02. Type/Customer

...
and so on...

How can I get this into a treeview? To loook like this:

- All
-- 01. Employees<b></b>
--- Administration


One of my tries:
C#
if (rs.RecordCount &gt; 0)
            {
                while (!rs.EOF)
                {
                    tempNode = tree;
                    string[] array = rs.Fields[&quot;GRUPPE&quot;].Value.ToString().Split(&#39;/&#39;);

                    foreach (string str in array)
                    {
                        if (tempNode.Nodes.ContainsKey(str))
                        {
                            tempNode = tree.Nodes[str];
                        }
                        else
                        {
                            TreeNode newNode = new TreeNode();
                            newNode.Name = str;
                            newNode.Text = str;

                            //*** PROBLEM!!!
                            tree.Nodes.Add(newNode);
                            //***
                            tempNode = newNode;
                        }
                    }

                    rs.MoveNext();
                }



Basically, my problem is how to can I add a Node on the 2nd or 3rd or Nrd level? Dynamically?

Hope you understand my problem?!

Kind Regards


Example of how it should be:

http://img198.imageshack.us/img198/5951/9zq.png[^
Posted
Updated 30-Jun-13 17:20pm
v2
Comments
Mahesh Bailwal 29-Jun-13 3:59am    
Below is the syntax to add child node.
TreeNode nodeParent = new TreeNode();
TreeNode nodeChild = new TreeNode();
nodeParent.Nodes.Add(nodeChild);
treeView1.Nodes.Add(nodeParent);
kaengraeng 29-Jun-13 4:32am    
thanks for your answer. unfortunately it doesnt help. When I have a string like this:
Employees/Administration/Teamleader

I want it to look like this in the treeview
Employees = 1st level
Administration = 2nd level
Teamleader = 3nd level

So how should i loop this string to add the children?
BillWoodruff 30-Jun-13 17:28pm    
The obvious mistake in your code ... assuming 'tree is the TreeView, and 'tempNode is a TreeNode ... is: tempNode = tree;

Second, you need to show more detail in your question on exactly how you wish the final output to appear.
kaengraeng 30-Jun-13 23:21pm    
thanks for the answer and please see the link above in my first page. Thats how it should be.

I created sample for this. Hope it will help

data.txt
Employees/Administration/Teamleader
Employees/Administration/Manager
Employees/Sales/Manager
Employees/Sales/Executive



string []str = File.ReadAllLines(@"D:\data.txt");
          TreeNode root = new TreeNode();
          root.Text = "All";

          foreach (string s in str)
          {
              string[] arr = s.Split('/');
              TreeNode department = null;
              if (!root.Nodes.ContainsKey(arr[1]))
              {

                  root.Nodes.Add(arr[1], arr[1]);
              }

            department =  root.Nodes[arr[1]];
            department.Nodes.Add(new TreeNode(arr[2]));
          }

          treeView1.Nodes.Add(root);
 
Share this answer
 
v2
Comments
kaengraeng 30-Jun-13 23:24pm    
thanks for your help, but your code is very static, which means if I would have a string like this: 'Employees/Administration/Teamleader/MisterA' the last part 'MisterA' will not be listed!
Ok i figured it out now. Please see below my code sample, which works fine for me so far.


string[] str = File.ReadAllLines(@"C:\test\test.txt");
            TreeNode root = new TreeNode();
            root.Text = "All";

            foreach (string s in str)
            {
                string[] arr = s.Split('/');
                TreeNode parent = root;

                foreach (string ss in arr)
                {
                    if (!parent.Nodes.ContainsKey(ss))
                    {
                        TreeNode newNode = new TreeNode();
                        newNode.Name = ss;
                        newNode.Text = ss;
                        parent.Nodes.Add(newNode);
                        parent = newNode;
                    }
                    else
                    {
                        parent = parent.Nodes[ss];
                    }
                }
            }
            treeView1.Nodes.Add(root);
            treeView1.ExpandAll();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900