Solved

Multiple conditions for adding tree nodes

Posted on 2008-10-23
5
796 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["location"].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 make_id
5) dealer: dealer_id

My primary key are not integers, they are alpha-numeric, so the code can differientiate between a model id and the dealer id for example.
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?
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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22789338
Seems there is a bug on this line:


                         if (!node.Nodes.ContainsKey(dr["dealer_id"].ToString()))   <<=== this should be "model" not dealer
                            {
                                TreeNode newNode = new TreeNode();
                                newNode.Text = dr["model"].ToString();
                                node.Nodes.Add(newNode);
                            }

Open in new window

0
 
LVL 6

Expert Comment

by:RishadanPort
ID: 22789362
I found that bug based on the consistency of what you previously had done...

all your if statements previously used this pattern of using the the specific level inside the if statement, however this statement, did not
0
 

Author Comment

by:MichelleLacy
ID: 22789406
see code snippet below, I copied/pasted incorrectly...

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);
                            }
                        }

Open in new window

0
 
LVL 25

Expert Comment

by:apeter
ID: 22789496
For each dealer nodes, you have loop the for car nodes

for each (...dealer loop)
{
   for each (car loop,  whose delaer ID is from above) 'this will get cars particular to that dealer.
}
0
 

Accepted Solution

by:
MichelleLacy earned 0 total points
ID: 22792947

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

617 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