2010-06-04 41 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