2010-06-04 57 views
0

我想創建一個使用自引用表字段的TreeView嵌套結構。下面是一個簡單的例子:使用自引用表創建TreeView嵌套結構表

Category 1 
     Product 1 
      Toy 1 
      Toy 2 
     Product 2 
      Toy 3 
      Toy 4 

多個類別..

數據庫表有稱爲 「A類」 單表。 ParentCategoryId指向父類別。因此,對於類別1,ParentCategoryId爲空,因爲它是父級。對於產品1,ParentCategoryId是類別1的id,對於玩具1,ParentCategoryId是產品1的id。

我使用下面的代碼,但它不能成功生成TreeView(ASP.NET)。

public void BuildTree(List<Category> categories, TreeNode treeNode) 
    { 
     if (treeNode == null) return; 

     TreeNode tnAdd = null; 
     var categoryId = Guid.NewGuid(); 

     foreach (var category in categories) 
     { 
      if (category.IsBaseCategory) 
      { 
       tnAdd = new TreeNode(); 
       tnAdd.Text = category.Description; 

       BuildTree((from c in categories 
          where c.ParentCategoryId == category.CategoryId 
          select c).ToList<Category>(), tnAdd); 
      } 
      else 
      { 
       tnAdd = new TreeNode(); 
       tnAdd.Text = category.Description; 

       BuildTree((from c in categories 
          where c.ParentCategoryId == category.CategoryId 
          select c).ToList<Category>(), tnAdd); 
      } 

      if (tnAdd != null) 
       treeNode.ChildNodes.Add(tnAdd);    
     } 
    } 

這是否需要遞歸!

和下面是結果我得到:

80W 
    40W 
    40W 
    Light Bulbs 

    Flourecent 
    Incedecent 

    60W 
    80W 
    60W 
    Flourecent 

    40W 
    80W 
    60W 

    Incedecent 

    80W 
    40W 
    60W 

回答

0

什麼是不成功? 如果因爲你看到沒有任何東西 ...我沒有看到你將根節點添加到實際的樹形控件中。 tnAdd需要添加到樹控件的某處。

如果這是因爲你沒有得到你所期望的一切:除非你已經有遞歸在某處並沒有意識到,否則我看不到上面的代碼是否會達到玩具級別。你在上面的代碼中說「base」,然後是「child」,它涵蓋了兩個層次。您的示例數據中有三個級別,因此在某些時候您需要考慮添加玩具。如果您需要有n的級別,可以遞歸地編寫它。如果你只有三個級別,你可以重複自己。

-----相關最新消息OP最新通報

你的代碼看,你這是什麼:

for each category { 
    if it is base 
     add its children 
    else if it is not base 
     add its children 

    add it to the tree 
    } 

這意味着每個項目打在第一的foreach並添加到樹而不是每個級別。 你想要的是

for each category{ 
    if it is base 
     add base's children 

     for each child [ 
      add child's children 
      add child to the tree 
     ] 

     add base the tree 
} 

東西接近這個(我沒有時間,現在,對不起測試)應該接近工作

public BuildTreeTop(List<Category> categories, TreeNode treeNode) 
{ 
    BuildTree((from c in categories 
          where c.IsBaseCategory == true 
          select c).ToList<Category>(), categories, tnAdd); 
} 

public void BuildTree(List<Category> currentLevel, List<Category> allCategories, TreeNode treeNode) 
    { 
     if (treeNode == null) return; 

     TreeNode tnAdd = null; 
     var categoryId = Guid.NewGuid(); 

     foreach (var category in currentLevel) 
     { 
      tnAdd = new TreeNode(); 
      tnAdd.Text = category.Description; 

      BuildTree((from c in allCategories 
         where c.ParentCategoryId == category.CategoryId 
         select c).ToList<Category>(), allCategories, tnAdd); 


      if (tnAdd != null) 
       treeNode.ChildNodes.Add(tnAdd);    
     } 
    } 
+0

感謝您的答覆!我更新了我的帖子!我認爲在主要應用中它可以達到n級。 – 2010-06-04 16:41:49

+0

你是個天才! – 2010-06-07 16:26:04

+0

還有一個幫助!我需要隱藏treeview控件的根節點。 – 2010-06-07 16:39:35