i have looked around the forum and on the web but couldnt find anything that seemed to be what i was looking for. or i couldnt follow how they where doing it.

What i need is to fill a treeview from a dataset. the dataset has been filled from a database.

when i want to fill a combobox all i need to do is

combo.DataSource = dtclimate;
            combo.DisplayMember = "name";
            combo.ValueMember = "id";

obviously the dataSource is a temp DataTable. that gets filled by a foreach loop.

i was wanting to do somthing simirler with a treeview but it has diffrent ways of doing things.

was wondering if someone could give me a good tut to look at or tell me a good way to handle a treeview and what everything stands for in the syntax. becuase it looks like its pretty diffrent logic and syntax for a treeview but i could be wronge.

thanks to all of you

6 Years
Discussion Span
Last Post by PierlucSS

You are not wrong, it is really different, since a treeview have different levels, which means a node, can contains many nodes, and each of these nodes cant contain even more nodes.

The solution to this is to build a recursive fonction, here's one I built a few weeks ago which is building a treeview from an xml.

Code to startup

treeView.Nodes.Add( new TreeNode( currDoc.DocumentElement.Name ) );

TreeNode tNode = new TreeNode();
tNode = treeView.Nodes[ 0 ];

XmlNode xNode = xmlDoc.DocumentElement;

AddNode(xNode , tNode);

Recursive function

private void AddNode( XmlNode inXmlNode , TreeNode inTreeNode )
                                XmlNode xNode;
                                XmlNodeList nodeList;
                                XmlElement xElement = (XmlElement)inXmlNode;

                                TreeNode tNode;
                                TreeNode tNode2;

                                if( inXmlNode.HasChildNodes )
                                        nodeList = inXmlNode.ChildNodes;
                                        for( int i = 0 ; i < nodeList.Count - 1 ; i++ )
                                                xNode = inXmlNode.ChildNodes[ i ];
                                                xElement = (XmlElement)xNode;
                                                tNode2 = new TreeNode( xElement.GetAttribute( "name" ) ); // will be the name of the node

                                                inTreeNode.Nodes.Add( tNode2 );
                                                tNode = inTreeNode.Nodes[ i ];
                                                AddNode( xNode , tNode );//recursive call


Hope this will help you :)


i realy did try to understand this lol but i really cant XD.

im useing a MS access databse to do it. so im not sure quite how to do it.

u am just not understanding how it is working. could you maybe describe it a little more? aslong as it donesnt bother you :)


In the first step, what I do is that I load the xml document and add a root node to the treeview ( to build up from that step ).

Then when I call the first AddNode method, I give it the treenode (which is a reference type and will update treeview even if you dont return any value) and the xml root node.

Then i'm doing a check to see if the current xml node have child nodes. If it does, I iterate through them. And for each node I'm adding, I'm calling again the AddNode function with the current xmlnode and treenode to verify if there is more child nodes and if there is, I go through the same iteration again.

When the code is at the deepest level (there's no more child nodes in the xml), it continues the previous iterations.

It's kinda hard to explain, hope this is a little clearer.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.