자체 참조 테이블 필드를 사용하여 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
감사 작업에 가까이 와야 이것에
뭔가 더 가깝다! 내 게시물을 업데이트했습니다! 나는 주 응용 프로그램에서 n 수준까지 갈 수 있다고 생각합니다. –
당신은 천재적 인 괴물입니다 !! –
한 번 더 도움이됩니다! treeview 컨트롤의 루트 노드를 숨길 필요가있다. –