Iterating through a treeView

Posted on 2006-06-16
Last Modified: 2012-05-05
Hi Experts,

      Maybe it's just me, but I think that working with the TreeView Control is 10 times harder than it has to be.  Here's what I need to do:

  I need to dynamically populate a treeView.  I DON'T WANT TO USE ANY LINKED LISTS OR ARRAY LISTS OR ANY OF THAT.  My app is way too complicated to make the use of lists practical.  I basically need to check if a certain grand child nood exists, if it does I need to update it, if not I need to create it.  Please keep in mind that I don't know if there's anything in the TreeView yet at all.  It could be completely empty in which case I need to make the Parent, Child, and, GrandChild nodes for that particular record.  If it does exist, then I need to update the grandchild's name.
  Also, How can I put an icon in front of a certaing grandchild node.  Not all of them, just certain ones.

Question by:axnst2
  • 4

Author Comment

ID: 16919645
I am working in C#

Author Comment

ID: 16919647
.NET 2005
LVL 85

Accepted Solution

Mike Tomlinson earned 500 total points
ID: 16919943

Author Comment

ID: 16920538
This is what I endedd up doing:

void FindInTreeView()
            bool equipAlreadyExists = false;
            int locatedEquipIndex = 0;

            if (treeView1.Nodes[0].Nodes.Count > 0)
            {// If TreeView isn't completely empty

                int equipCounter=0;

                // Check to see if Equipment is already in the list
                foreach (TreeNode tn in treeView1.Nodes[0].Nodes)
                    if (tn.Text == tempEquip.IPAddress)
                    {// It exists
                        equipAlreadyExists = true;
                        locatedEquipIndex = equipCounter;

                // If it exists, update it
                if (equipAlreadyExists)
                    treeView1.Nodes[0].Nodes[locatedEquipIndex].Nodes[0].Text = "Equipment Type: " + tempEquip.EquipType;
                    treeView1.Nodes[0].Nodes[locatedEquipIndex].Nodes[1].Text = "Comm. Status: " + tempEquip.CommStatus;
                    treeView1.Nodes[0].Nodes[locatedEquipIndex].Nodes[2].Text = "Comm. Time: " + tempEquip.LastCommTime + " ms";
                else // If equipment doesn't exist, add it
                    treeView1.Nodes[0].Nodes[equipCounter].Nodes.Add("Equipment Type: " + tempEquip.EquipType);
                    treeView1.Nodes[0].Nodes[equipCounter].Nodes.Add("Comm. Status: " + tempEquip.CommStatus);
                    treeView1.Nodes[0].Nodes[equipCounter].Nodes.Add("Comm. Time: " + tempEquip.LastCommTime + " ms");

            else// If TreeView is empty

                // Add new equipment
                treeView1.Nodes[0].FirstNode.Nodes.Add("Equipment Type: " + tempEquip.EquipType);
                treeView1.Nodes[0].FirstNode.Nodes.Add("Comm. Status: " + tempEquip.CommStatus);
                treeView1.Nodes[0].FirstNode.Nodes.Add("Comm. Time: " + tempEquip.LastCommTime + " ms");

This works well for me.  TriewWiev still stinks though!

Author Comment

ID: 16920562
Thanks for your help though Idle!

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
XAML: Layout 8 25
About delegates in c sharp 3 35
Error when loading the database 16 57
System.Speech 2 16
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Sending a Secure fax is easy with eFax Corporate ( First, just open a new email message. In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

895 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now