Solved

Add TreeNodes dynamically

Posted on 2008-10-23
4
1,579 Views
Last Modified: 2013-12-17
I have a tree veiw with multiple levels, populated from a database.  The first level is the country the builds the car.

foreach (DataRow dr in concepts.Rows)
                    {
                        if (!treeView1.Nodes.ContainsKey(dr["country_id"].ToString()))
                        {
                            treeView1.Nodes.Add(dr["country_id"].ToString(), dr["country_name"].ToString());
                        }
                    }

//The second level is the make of the car

foreach (DataRow dr in concepts.Rows)
                    {
                        TreeNode[] nodes = treeView1.Nodes.Find(dr["country_id"].ToString(), true);
                        if (nodes.Length > 0)
                        {
                            TreeNode node = nodes[0];
                            if (!node.Nodes.ContainsKey(dr["make_id"].ToString()))
                            {
                                node.Nodes.Add(dr["make_id"].ToString(), dr["make_name"].ToString());
                            }
                        }
                    }

//The third level is the car dealership

foreach (DataRow dr in concepts.Rows)
                    {
                        TreeNode[] nodes = treeView1.Nodes.Find(dr["make_id"].ToString(), true);
                        if (nodes.Length > 0)
                        {
                            TreeNode node = nodes[0];
                            if (!node.Nodes.ContainsKey(dr["dealer_id"].ToString()))
                            {
                                node.Nodes.Add(dr["dealer_id"].ToString(), dr["year"].ToString());
                            }
                        }
                    }

//and the final level is the model

foreach (DataRow dr in concepts.Rows)
                    {
                        TreeNode[] nodes = treeView1.Nodes.Find(dr["dealer_id"].ToString(), true);
                        if (nodes.Length > 0)
                        {
                            TreeNode node = nodes[0];
                            if (!node.Nodes.ContainsKey(dr["dealer_id"].ToString()))
                            {
                                TreeNode newNode = new TreeNode();
                                newNode.Text = dr["model"].ToString();
                                node.Nodes.Add(newNode);
                            }
                        }
                    }

My problem is in the last two nodes.  No matter what dealer has the car,  all models are added to the first dealer in the tree.  I think it has to due with the schema of my database.  I have five tables.

1) country: PK countryID
2) make: PK make_id, FK countryID
3)model: PK model_id FK dealer_id
4) car properties: PK car_id, FK model_id, FK car_id
5) dealer: dealer_id

How do I get the code to add the appropriate car to the appropriate dealer node without changing the schema structure?
0
Comment
Question by:MichelleLacy
  • 3
4 Comments
 
LVL 7

Expert Comment

by:dctuck
Comment Utility
If you are using an integer as the primary key for tables, each table's first record will have an Identity of 1, so your TreeNode will also have a key of 1 for the first item in each section (hope that makes sense!)
The way I would do it, is to append the ID of the item with the type of item it is, for example, for a make, you could use:
node.Nodes.Add("make_" + dr["make_id"].ToString(), dr["make_name"].ToString());
and for a dealer:
node.Nodes.Add("dealer_" + dr["dealer_id"].ToString(), dr["year"].ToString());
This means the TreeView can differentiate between the different types, so when you search, you have to make sure you use something like:
if (!node.Nodes.ContainsKey("dealer_" + dr["dealer_id"].ToString()))
{
     node.Nodes.Add("dealer_" + dr["dealer_id"].ToString(), dr["year"].ToString());
}
And so on. Hope that helps
Daniel
0
 

Author Comment

by:MichelleLacy
Comment Utility
sorry there is a couple of typos in the code "year" should be "location".  and the table schema is
1) country: PK countryID
2) make: PK make_id, FK countryID
3) model: PK model_id FK dealer_id
4) car properties: PK car_id, FK model_id, FK make_id
5) dealer: dealer_id
However, my primary key are not integers, they are alpha-numeric, so the code can differientiate between for example a model id and the dealer id.
Is there a way that I can add a condition that tells it to not only look for the dealer id for the mdoel, but also one node up from that, the make_id.   So that it has pass both conditions (be the right make as well be present at a specific dealership?
0
 

Author Comment

by:MichelleLacy
Comment Utility
I tried a couple of different approaches to solving this problem.  I tried nesting the foreach loops for the last two queries, then I tried with the last three queries so that in order to add a model node, the method would first have to find the make as well as the dealer.  Neither approach worked.  
After looking at my schema model, there is not a direct relationship between my make table and my dealer table.  So there does not appear to be a straightforward way to build a tree with 1st node being country, then make, then dealer, then model.  So I changed the layout of my tree view and got rid of the dealer node altogether.  I added dealer in a separate control drop down box that the users could filter on to narrow down what is shown in the tree.
0
 

Accepted Solution

by:
MichelleLacy earned 0 total points
Comment Utility

I tried a couple of different approaches to solving this problem.  I tried nesting the foreach loops for the last two queries, then I tried with the last three queries so that in order to add a model node, the method would first have to find the make as well as the dealer.  Neither approach worked.  
After looking at my schema model, there is not a direct relationship between my make table and my dealer table.  So there does not appear to be a straightforward way to build a tree with 1st node being country, then make, then dealer, then model.  So I changed the layout of my tree view and got rid of the dealer node altogether.  I added dealer in a separate control drop down box that the users could filter on to narrow down what is shown in the tree.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

772 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

15 Experts available now in Live!

Get 1:1 Help Now