• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 344
  • Last Modified:

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.
0
angus_young_acdc
Asked:
angus_young_acdc
1 Solution
 
AndyAinscowCommented:
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
 
AndyAinscowCommented:
       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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
AndyAinscowCommented:
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
 
AndyAinscowCommented:
            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
 
AndyAinscowCommented:
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
 
AndyAinscowCommented:
Making copies of data is a BAD IDEA.  The data already exists in a structured collection - the tree and the nodes it contains.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now