Stopping Duplicates in TreeView

Hello,

I'm attempting to call a WebService to return a list of clients and their subsiduaries, these are returned individually as part of a list.  So for example, client NobodyCorp has 3 sub-companies, Nope, Nothing and none.  So this would be returned as:

ClientName = NobodyCorp
Company = Nope

ClientName = NobodyCorp
Company = Nothing

ClientName = NobodyCorp
Company = None

I'm then hoping to add these to a treeview so that they appear as follows:

NobodyCorp
- Nope
- Nothing
- None

However I'm struggling to stop it adding separate parent trees for NobodyCorp.  I've tried to state if this node already exists, then try and add a child, but I've really gotten nowhere.

Can some help please be provided?  I have googled for this but can't seem to return anything useful other than adding it and foreaching through the list.

Or if there was a method that I could add the details to a 2 dimension list, and for any client which is already in the list just add its sub companies to that index in the list.
LVL 15
angus_young_acdcAsked:
Who is Participating?
 
AndyAinscowFreelance programmer / ConsultantCommented:
            TreeNode tvn = null;
            foreach (TreeNode tn in this.treeView1.Nodes)
            {
                if (tn.Text == "Parent")
                {
                    tvn = tn;
                    break;
                }
            }
            if (tvn != null)
                tvn.Nodes.Add("Hello");  //Add child
            else
            {
                //Add the parent and child
            }

Open in new window


The above code might be of use.  There is a Find function as a TreeView member but it didn't seem to work for some reason!
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
When you have a child node you need to specify the parent node it belongs to.  It sounds like you are specifically creating a new parent node every time you attempt to add the child rather than reusing the parent.
0
 
angus_young_acdcAuthor Commented:
How can I re-use the parent?
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
AndyAinscowFreelance programmer / ConsultantCommented:
       private void button1_Click(object sender, EventArgs e)
        {
            TreeNode tvn = this.treeView1.Nodes.Add("Parent");
            tvn.Nodes.Add("Child1");
            tvn.Nodes.Add("Child2");
            tvn.Nodes.Add("Child3");
           
            tvn = this.treeView1.Nodes.Add("Another Parent");
            tvn.Nodes.Add("Child x");
            tvn.Nodes.Add("Child y");
            tvn.Nodes.Add("Child z");
        }

0009.jpg
0
 
angus_young_acdcAuthor Commented:
I'm already able to add them like that, which works fine, my issue is that if I have numerous objects in the returned list I don't want duplicate parent names.  So in my example above of the 3 different items with the same client name, I would want to have 1 client name as the parent but all 3 of the sub companies added under this.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
As I said earlier, add the clients to that one parent.
0
 
Fernando SotoRetiredCommented:
Hi angus_young_acdc;

You state that, "I'm attempting to call a WebService to return a list of clients and their subsiduaries, these are returned individually as part of a list".

What is the return type of the data, List<CustomObject>, List<String> - formatted how?
What version of Visual Studio?
What platform, Windows Forms or Web?

Fernando
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
ps.  It is coded to work with my sample earlier - two nodes ("Parent" and "Another Parent") each with three child nodes.
0
 
davesgonebananasCommented:
I think you need to use an algorithm similar to the following:

1

Maintain a Dictionary<string, TreeNode> of client name's and nodes.  

2

Create the company node

3

Before Inserting the company get parent node by checking if the client name is in the dictionary.

4

If not, create the parent node and add to dictionary

5

Add company node to the parent node
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
Making copies of data is a BAD IDEA.  The data already exists in a structured collection - the tree and the nodes it contains.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.