Solved

Add TreeNodes dynamically

Posted on 2008-10-23
4
1,588 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
ID: 22786926
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
ID: 22787469
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
ID: 22792939
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
ID: 22792941

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 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