Click here to Skip to main content
15,890,043 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am new to treeview control
i have table tree.mdb

table contents
id    number
name  text
refid number


values are
id      name           refid
_____________________________
1001     a             1000
1002     b             1001
1003     c             1002
1004     d             1003
1005     e             1001


i want display the above table in tree view as

1000
  |
  1001
    |
    1002
    | |
    | 1003
    |   |
    |   1004
    |
    1005


How can get the above tree view

thanks in advance
Dineshkumar R
Posted
Comments
Sergey Alexandrovich Kryukov 24-Jul-11 15:01pm    
fail to see any logic in you mapping to the tree. Why on Earth 1002 is branched into 1005 and 1003, while other node having one child; why this structure at all?
--SA

The root nodes can be added to the TreeView using its Nodes collection.
C#
TreeNode root1 = treeView1.Nodes.Add("Animal", "Animal");

Similarly, each TreeNode has a Nodes collection. To add sub nodes to the tree, add your new TreeNode object to the appropriate TreeNode's Nodes collection.
C#
TreeNode level1SubRoot1 = root1.Nodes.Add("Dogs", "Dogs");
TreeNode level2SubRoot1 = level1SubRoot1.Nodes.Add("GermanShepard", "German Shepard");
TreeNode level2SubRoot2 = level1SubRoot1.Nodes.Add("Labarador", "Labarador");


To populate your TreeView from the database, read your table in a DataReader (ORDER BY ParentID ASC), loop through the reader, if the Parent ID is null, add it to the TreeView's Nodes collection, otherwise find the TreeNode using the TreeView.Nodes.Find() method and then add it to the found node's Nodes collection.

C#
while dr.Read() {
    if (dr["refid"] == DBNull.Value) {
        treeView1.Nodes.Add(dr["id"].ToString(), dr["name"].ToString());
    } else {
        TreeNode node = treeView1.Nodes.Find(dr["refid"].ToString(), true);
        if (node != null) {
            node.Nodes.Add(dr["id"].ToString(), dr["name"].ToString());
        }
    }
}

NOTE: dr is the DataReader that has already been opened.
 
Share this answer
 
v2
Assuming that you need to make this generic, i.e. that your database may contain other results, the first thing to do is to create a framework that reads the database and calls a method to add each node in turn. The hardwired version of this is:
AddNode(myTreeView, 1001, "a", 1000);
AddNode(myTreeView, 1002, "b", 1001);
AddNode(myTreeView, 1003, "c", 1002);
AddNode(myTreeView, 1004, "d", 1003);
AddNode(myTreeView, 1005, "e", 1001);
So, you need to write the AddNode method. But first, how are you going to store this? A TreeNode can only store text, and a Tag value. I would suggest that you first create a class to hold your node information, and put that into your TreeNode Tag:
public class myNode
    {
    public int ID { get; set; }
    public string Name { get; set; }
    public myNode(int id, string name)
        {
        ID = id;
        Name = name;
        }
    }
Nice and simple, but can be expanded.
Now, AddNode is simple, but since this is your homework, I'm not giving you the actual code!
1) Create a new myNode instance.
2) Create a new TreeNode instance. Set the new TreeNode.Tag property to the myNode instance
3) Find the parent node you need to attach to.
4) If it exists, add a new node to it. Otherwise, add the new node to the TreeView root.

Find the parent node is slightly more complex: you need recursion, so create a new method:
private TreeNode FindNode(TreeNodeCollection nodes, int id)

You will initially call this with the Nodes property of your TreeView:
TreeNode t = FindNode(treeView.Nodes, parentId);

FindNode isn't difficult either:
1) If nodes has no items, return null - it isn't in this list.
2) Loop through each node in nodes (look at a foreach loop)
2a) Cast the node.Tag property to a myNode instance (it is a very good idea to check it is a myNode before you try to use it)
2b) If the ID property matched the ID you are looking for, return this node.
2c) Otherwise, call FindNode on the node.Nodes property of this node.
2d) If the node is found, return it. (this passes the found node all the way back up the chain)
2e) Otherwise, try the next node in the list
3) If you run out of nodes without finding the ID you are looking for, return null.

Sounds complex? A bit. But in practice, the code for each part is pretty simple, and actually takes less space that the description above!

Try it - it isn't really difficult, and if you get stuck, ask!
 
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