Link to home
Start Free TrialLog in
Avatar of MichelleLacy
MichelleLacy

asked on

Add TreeNodes dynamically

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?
Avatar of dctuck
dctuck
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of MichelleLacy
MichelleLacy

ASKER

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?
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.
ASKER CERTIFIED SOLUTION
Avatar of MichelleLacy
MichelleLacy

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