By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,946 Members | 773 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,946 IT Pros & Developers. It's quick & easy.

TreeView-

P: 18
this is my data
CategName subCateName Id Products
NULL NULL 6 Prod3
NULL NULL 9 SalesProd7
Beverages Beers 15 Bell Lager 500 ML
Beverages Beers 16 CLUB PILSENER 500 ML
Beverages Juices 24 Apple Juice
Beverages Juices 25 Chikooo Juice
Beverages SoftDrinks 26 Coke
Beverages SoftDrinks 27 Pepsi
Beverages Wines 28 RED WINE
Beverages Wines 29 White Wine
Food MeatProducts36 GOAT MEAT
Food Fruits 18 APPLE GREEN
Food Fruits 19 LOCAL ORANGES
Others OtherItems 30 BENSON & HEDGES LIGHT
VegeTable NULL 20 BEETROOT
VegeTable NULL 21 TOMATO FRESH

I got the above data into Datatable 'table' and I am trying to insert into tree view
as Categories-subcategories-Products. I have tried the following and my output in such a way i get only
one product for each categorie and sub categorie.

Expand|Select|Wrap|Line Numbers
  1. if (table != null)
  2.             {
  3.                 foreach (DataRow row in table.Rows)
  4.                 {
  5.                     MainNode = new TreeNode(row.ItemArray[0].ToString());
  6.                     MainNode.Name = row.ItemArray[0].ToString();
  7.                     if (MainNode.Text == "")
  8.                     {
  9.                         MainNode = new TreeNode(row.ItemArray[1].ToString());
  10.                         MainNode.Name = row.ItemArray[1].ToString();
  11.                         if (MainNode.Text == "")
  12.                         {
  13.                             MainNode = new TreeNode(row.ItemArray[3].ToString());
  14.                             MainNode.Name = row.ItemArray[3].ToString();
  15.                             MainNode.Tag = row.ItemArray[2].ToString();
  16.                             if (!RootNode.Nodes.ContainsKey(MainNode.Name))
  17.                             {
  18.                                 RootNode.Nodes.Add(MainNode);
  19.                             } }
  20.                         else
  21.                         {
  22.                             ChildNode = new TreeNode(row.ItemArray[3].ToString());
  23.                             ChildNode.Name = row.ItemArray[3].ToString();
  24.                             ChildNode.Tag = row.ItemArray[2].ToString();
  25.                             if (!MainNode.Nodes.ContainsKey(ChildNode.Name))
  26.                             {
  27.                                 MainNode.Nodes.Add(ChildNode);
  28.                             }
  29.                             if (!RootNode.Nodes.ContainsKey(MainNode.Name))
  30.                             {
  31.                                 RootNode.Nodes.Add(MainNode);
  32.                             } } }
  33.                     else
  34.                     {
  35.                         ChildNode = new TreeNode(row.ItemArray[1].ToString());
  36.                         ChildNode.Name = row.ItemArray[1].ToString();
  37.                         if (ChildNode.Text == "")
  38.                         {
  39.                             ChildNode = new TreeNode(row.ItemArray[3].ToString());
  40.                             ChildNode.Name = row.ItemArray[3].ToString();
  41.                             ChildNode.Tag = row.ItemArray[2].ToString();
  42.                             if (!MainNode.Nodes.ContainsKey(ChildNode.Name))
  43.                             {
  44.                                 MainNode.Nodes.Add(ChildNode);
  45.                             } }
  46.                         else
  47.                         {
  48.                             SubNode = new TreeNode(row.ItemArray[3].ToString());
  49.                             SubNode.Name = row.ItemArray[3].ToString();
  50.                             SubNode.Tag = row.ItemArray[2].ToString();
  51.                             if (!ChildNode.Nodes.ContainsKey(SubNode.Name))
  52.                             {
  53.                                 ChildNode.Nodes.Add(SubNode);
  54.                             } }
  55.                         if (!MainNode.Nodes.ContainsKey(ChildNode.Name))
  56.                         {
  57.                             MainNode.Nodes.Add(ChildNode);
  58.                         }
  59.                         else
  60.                         {
  61.                         //Here I am unable to understand how to fill child nodes into mainnode
  62.                         //The below code is wrong 
  63.                             for (int i = 0; i <= MainNode.Nodes.Count; i++)
  64.                             {
  65.                                 MainNode.Nodes.Add(ChildNode);                                
  66.                             }                   }
  67.                         if (!RootNode.Nodes.ContainsKey(MainNode.Name))
  68.                         {
  69.                             RootNode.Nodes.Add(MainNode);                            
  70.                         }                    }                }            }
Code complexity is more here...(worst) Is there any other way of implementing
like Switch or any other methods.
Feb 12 '09 #1
Share this Question
Share on Google+
2 Replies


Plater
Expert 5K+
P: 7,872
So you are trying to create a tree that is something like:
Expand|Select|Wrap|Line Numbers
  1. |-Beverages
  2. |       |-Beers
  3. |            |-Bell Lager 500 ML
  4. |            |-CLUB PILSENER 500 ML
  5. |       |-Juices
  6. |            |-Apple Juice
  7. |            |-Chikooo Juice
  8. |       |-SoftDrinks
  9. |            |-Coke
  10. |            |-Pepsi
  11. |       |-Wines
  12. |            |-RED WINE
  13. |            |-White Wine
  14. |-Food
  15. |       |-MeatProducts
  16. |            |-GOAT MEAT
  17. |       |-Fruits
  18. |            |-APPLE GREEN
  19. |            |-LOCAL ORANGES
  20. |-Others
  21. |       |-OtherItems
  22. |            |-BENSON & HEDGES LIGHT
  23. |-VegeTable
  24. |       |-NULL
  25. |            |-BEETROOT
  26. |            |-TOMATO FRESH
  27.  
Feb 12 '09 #2

P: 18
Yaa exactly I need in that Way.
But I am unable to....I have got in these way only

Beverages
Beers
Club Pilesener
Here I am unable to display one more Product
Feb 15 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.