2010-06-04 4 views
0

자체 참조 테이블 필드를 사용하여 TreeView 중첩 구조를 만들려고합니다.자체 참조 테이블을 사용하여 TreeView 중첩 구조 만들기

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

이상의 범주 ..

데이터베이스 테이블이 "종류"라는 하나의 테이블을 가지고 : 다음은 간단한 예입니다. ParentCategoryId는 부모 인 Category를 가리 킵니다. 따라서 Category 1의 경우 ParentCategoryId는 부모이므로 null입니다. 제품 1의 경우 ParentCategoryId는 Category 1 id의 것이고 Toy 1의 경우 ParentCategoryId는 Product 1 id의 ParentCategoryId입니다.

다음 코드를 사용하고 있지만 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는 어딘가에 트리 컨트롤에 추가되어야합니다.

당신이 기대하는 모든 것을 얻지 못하기 때문입니다. 이미 반복적 인 어딘가에 가서 깨닫지 못한다면, 위의 코드가 어떻게 장난감 수준에 도달하는지 알 수 없습니다. 위의 코드에서 두 단계를 다루는 "기본", "아동"이라고 말합니다. 샘플 데이터에는 세 가지 레벨이 있으므로 어떤 시점에서 장난감 추가에 대한 설명이 필요합니다. n 레벨이 필요할 경우 재귀 적으로 작성할 수 있습니다. 3 단계 만있는 경우 반복 할 수 있습니다. OP UPDATES

당신의 코드를 찾고에 대한

----- 업데이트, 당신이해야하는 것은 이것이다 :

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 문에 명중하고 트리에 추가 의미 , 수준보다는 오히려. 당신이 원하는 것은 (지금은 죄송 테스트 할 시간이 없어) 답장을

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

감사 작업에 가까이 와야 이것에

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 } 

뭔가 더 가깝다! 내 게시물을 업데이트했습니다! 나는 주 응용 프로그램에서 n 수준까지 갈 수 있다고 생각합니다. –

+0

당신은 천재적 인 괴물입니다 !! –

+0

한 번 더 도움이됩니다! treeview 컨트롤의 루트 노드를 숨길 필요가있다. –