Link to home
Start Free TrialLog in
Avatar of angus_young_acdc
angus_young_acdcFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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.
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

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.
Avatar of angus_young_acdc

ASKER

How can I re-use the parent?
       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
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.
As I said earlier, add the clients to that one parent.
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
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ps.  It is coded to work with my sample earlier - two nodes ("Parent" and "Another Parent") each with three child nodes.
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
Making copies of data is a BAD IDEA.  The data already exists in a structured collection - the tree and the nodes it contains.