Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 30th, 2012  |  syntax: None  |  size: 3.36 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. how do i go from a datatable to a hierarchical structure in asp.net c#
  2. BranchId     ParentId    ProductCount    HasBranchChildren       Name
  3. 0           NULL        48              1                       Categories
  4. 1           0           20              1                       CategoryA
  5. 2           1           10              1                       CategoryA1
  6. 3           1           8               0                       CategoryA2
  7. 4           1           2               0                       CategoryA3
  8. 5           2           4               0                       CategoryA1.1
  9. 6           2           6               0                       CategoryA1.2
  10. 7           0           28              1                       CategoryB
  11. 8           7           20              0                       CategoryB1
  12. 9           7           8               0                       CategoryB2
  13.        
  14. public class Branch
  15. {
  16.     public Branch()
  17.     {
  18.         Children = new List<Branch>();
  19.     }
  20.  
  21.     public bool IsRoot { get; set; }
  22.     public int? BranchId { get; set; }
  23.     public int? ParentId { get; set; }
  24.     public List<Branch> Children { get; set; }
  25.  
  26.     public int ProductCount { get; set; }      
  27.     public int HasBranchChildren { get; set; }
  28.  
  29.     public string Name { get; set; }
  30.     //other data
  31. }
  32.  
  33. public class BranchTreeHelper
  34. {
  35.     public Branch MakeTree()
  36.     {
  37.         var virtualRoot = new Branch()
  38.         {
  39.             BranchId = null,
  40.             ParentId = null,
  41.             IsRoot = true
  42.         };
  43.  
  44.         // get the data from db, just disregard the properties: Children and IsRoot flage
  45.         List<Branch> branchList = GetDataFromDB();
  46.  
  47.         var branchDict = branchList.ToDictionary(i => i.BranchId.Value, i => i);
  48.  
  49.         foreach(var branch in branchList)
  50.         {
  51.             if(branch.ParentId.HasValue)
  52.             {
  53.                 branchDict[branch.ParentId.Value].Children.Add(branch);
  54.             }
  55.             else
  56.             {
  57.                 virtualRoot.Children.Add(branch);
  58.             }
  59.         }
  60.  
  61.         return virtualRoot;
  62.     }
  63. }
  64.        
  65. // just a concept, not tested, might have errors
  66.  
  67. // ObjectWithHierarchy could look something like this
  68. class ObjectWithHierarchy
  69. {
  70.     public int Id {get;set;}
  71.     public int? ParentId {get;set;}
  72.     public string Name {get;set;}
  73. }
  74.  
  75. private void LoadTree()
  76. {
  77.     // initialize your collection of objects
  78.     List<ObjectWithHierarchy> list = new List<ObjectWithHierarchy>();
  79.  
  80.     // build the tree
  81.     TreeView treeView = new TreeView();
  82.  
  83.     foreach (var item in list.FindAll(i => i.ParentId == null))
  84.     {
  85.         // create the root node
  86.         TreeNode rootNode = new TreeNode();
  87.         rootNode.Text = item.Name;
  88.         rootNode.Tag = item;
  89.  
  90.         // load child nodes
  91.         LoadChildNodes(node, list);
  92.  
  93.         // add the node to the treeview
  94.         treeView.Nodes.Add(rootNode);
  95.     }
  96. }
  97.  
  98. private void LoadChildNodes(TreeNode node, List<ObjectWithHierarchy> list)
  99. {
  100.     ObjectWithHierarchy item = node.Tag as ObjectWithHierarchy;
  101.     foreach (var childItem in list.FindAll(i => i.ParentId == item.Id))
  102.     {
  103.         // create the child node
  104.         TreeNode childNode = new TreeNode();
  105.         childNode.Text = childItem.Name;
  106.         childNode.Tag = childItem;
  107.  
  108.         // load children, if there are any
  109.         LoadChildNodes(childNode, list);
  110.  
  111.         // add it to its parent nodes collection
  112.         node.Nodes.Add(childNode);
  113.     }
  114. }