Solved

Multiple conditions for adding tree nodes

Posted on 2008-10-23
5
789 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
  • 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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…

760 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

21 Experts available now in Live!

Get 1:1 Help Now