Need more explaination on Treeviews

Posted on 2003-02-24
Medium Priority
Last Modified: 2010-04-15
I need more explanation on the treeview.
How can I select a Nodes?

Example :


I would like List each element of the treeview and i want to be able to insert a level in a node.

Ex: Level1_0_0


Question by:DrChal
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3

Accepted Solution

ReAdm earned 60 total points
ID: 8010345
I'm not entirely sure what you mean, but I have a resusable routine to load a treeview from an XML document and control the nodes from there, using XML attributes to provide text, image color etc. properties for the node and tying an id attribute to the node key. This has several advantages mainly because you can store additional node information in the XML, and best of all use XPath to get any subsets of nodes, counts of nodes ect. XPath makes it easy to find things like 'Level1_0_0' with out recursing through all the nodes.
Manipulating all the data in the treeview itself, whilst completely possible, usually turns out very messy with lots of recursive routines to iterate through the nodes. The XML document provides all the methods you need for adding, moving ,removing and searching elements, which can be easily mapped to nodes. It seems like more at first, but by the second or third time you use it you realise how much easier it is.
If you are not familiar with XML and XPath it is well worth the effort.

Expert Comment

ID: 8014867
Which treeview control did you use?
As far as I know, treeview control is not currently supported.

"This product is only compatible with machines running builds of the .NET Framework (1.0.3705.0) and Visual Studio® .NET (7.0.9466)."

This can be read here(where you can download the treeview too):



Author Comment

ID: 8015004
Thanks for your explanation.
But i'm a beginner in C#. Have you got an example of code for filling a treeview with XML File.



Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Expert Comment

ID: 8015706
ibai, I'm just using the standard System.Windows.Form.TreeView

okay here's an example:
This uses a saved xml file ("c:/xmlfile1.xml") for practical use you'll probably want to build one on the fly, (this isn't the most straight forward thing to as the xml document has long winded ways of adding nodes and attributes, however it is easy to write very simple wrapper functions to do these for you and these will serve you well for many things)

Tree loading code:

          private void LoadTree()

               XmlDocument xmldoc =new XmlDocument();
               xmldoc.Load ("c:/xmlfile1.xml");

          private void RecurseLoadNode(TreeNodeCollection tnodes,XmlNode xmlnode)
               foreach (XmlNode childnode in xmlnode.ChildNodes)
                    TreeNode tn =tnodes.Add(childnode.Attributes.GetNamedItem("text").Value);
                    if (childnode.ChildNodes.Count > 0)

with this xml document:

<?xml version="1.0" encoding="utf-8" ?>
     <treenode id="a" text="firstnode">
          <treenode id="b" text="firstnodefirstchild"></treenode>
          <treenode id="c" text="firstnodesecondchild">
               <treenode id="n" text="firstnodesecondchildgrandchild1"></treenode>
               <treenode id="o" text="firstnodesecondchildgrandchild2"></treenode>
               <treenode id="p" text="firstnodesecondchildgrandchild3"></treenode>
          <treenode id="d" text="firstnodethirdchild"></treenode>
          <treenode id="e" text="firstnodeforthchild"></treenode>
          <treenode id="f" text="firstnodefifthchild"></treenode>
     <treenode id="g" text="secondnode">
          <treenode id="h" text="secondnodesecondchild"></treenode>
          <treenode id="i" text="secondnodesecondchild"></treenode>
          <treenode id="j" text="secondnodethirdchild"></treenode>
          <treenode id="k" text="secondnodeforthchild">
               <treenode id="q" text="secondnodeforthchildgrandchild1"></treenode>
               <treenode id="r" text="secondnodeforthchildgrandchild2"></treenode>
               <treenode id="s" text="secondnodeforthchildgrandchild3"></treenode>
          <treenode id="l" text="secondnodefifthchild"></treenode>
          <treenode id="m" text="secondnodesixthchild"></treenode>

This does have a learning overhead of needing to understand the XML document model, but you'll probably want to learn this anyway.



Expert Comment

ID: 8015736
Sorry, I was thinking about ASP.NET treeview.
Did you know any control like it for ASP.NET?
As I said before, the MS treeview is not supported more.
Any idea?

Expert Comment

ID: 8015833
Ibai, I've not had anything to do with ASP.NET yet, but I have written an XSLT to convert a tree XML as above to HTML/Javascript that will display a tree in a browser, expand/collapse, highlight and detect clicks as you would expect. Its  a bit much to explain here, but if you're familiar with XSL well worth looking into.

Expert Comment

ID: 8015855
Could you send me the xslt please?
my email is igarle#@#hotmail.com
without # of course

Author Comment

ID: 8016287
Thank you ReAdm,
With this example, i can do that i want to make.


Author Comment

ID: 8016918
Can I ask you another question?
How have you built your Xlm File?
Do you use this syntax :
DataSet DsXml;
FileStream file=new FileStream("resultat.xml", FileMode.OpenOrCreate, FileAccess.Write);
//Write File              
DsXml.WriteXml (file);


Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question