绑定treeView与递归 [英] Binding treeView with recursion
问题描述
你好,我正在研究Visual Sudio,这是我第一次使用treeView。我的项目有一个ADO .NET实体数据模型和SQL数据库,我有3个关系表:
Hello, I''m working on Visual Sudio and this is the my first time with treeView. My project has a ADO .NET entity data model of and SQL database, I have 3 tables with relations:
Category (idCategory, nameCategory)
subCategory (idSubCategory, nameSubCategorie, idCategory)
Articles (idArticle, nameArticle, idSubCategory)
我正在尝试用这个数据库中的递归创建一个treeView,最后得到结果如下,我被困在这里!有人可以帮帮我吗?
这是我要在树视图中显示的内容:
I''m trying to create a treeView with recursion from this database to finally get a result like below, I''m stuck here! Someone can help me please?
Here is what I want to show in treeView:
+Category1
+subCategory1-1
article1
article2
article3
+subCategory1-2
article1
article2
article3
+subCategory1-3
article1
article2
article3
+Category2
+subCategory2-1
article1
article2
article3
+subCategory2-2
article1
article2
article3
+subCategory2-3
article1
article2
article3
用于绑定递归treeView的模板代码来自数据库usinf LINQ或教程链接,显示如何做到这将非常有帮助!谢谢。
A template code for binding recursive treeView from database usinf LINQ or a tutorial link that shows how to do that will be very helpfull! Thanks.
推荐答案
您可以将所有数据放入类中,就像您的情况一样
class CategoryCalss {public string idCat; public string nameCat}
class suCateClass {public string idSubCat; public string nameSubCat; public string idCat}
class ArticClass {public string idArt,public string name,public string idSubCat}
获取数据并将数据填写到相应的类和表格列表< category> ;, List< su .....,>
然后你可以使用下面的代码
TreeNode treeNodeParent = new TreeNode();
TreeNode treeNodeChd = new TreeNode();
foreach(CategoryClass CatList中的猫)
{
treeNodeChd = new TreeNode();
treeNodeChd = getsubDir(cat);
treeNodeParent.ChildNodes.Add(treeNodeChd);
}
TreeView1.Nodes.Add(treeNodeParent);
public TreeNode getSubCat(CategoryClass cat)
{
IEnumerable< subcatclass> subcatQuery =来自subcat的subCa,其中subCa.idCat = cat.idCat select subCa;
TreeNode par = new TreeNode();
TreeNode Chd = new TreeNode();
foreach(Subcatquery中的SubCatclass sub){<
chd = new TreeNode();
chd = getArtl(sub);
chd.Text = sub.name;
par.ChildNodes.Add(chd);
}
返回标准杆;
}
public TreeNode getArtl(subcatClass cat)
{
IEnumerable< artlclass> artlQuery =来自ArtlList中的artl,其中artl.idCat = cat.idsubCat select artl;
TreeNode par = new TreeNode();
TreeNode Chd = new TreeNode();
foreach(artlQuery中的ArtlClass art){<
chd = new TreeNode();
chd.Text = art.name;
par.ChildNodes.Add(chd);
}
返回标准杆;
}
以上是我要回复的粗略代码。这应该对你有所帮助。
You can put all the data into class like in your case
class CategoryCalss{public string idCat;public string nameCat}
class suCateClass{public string idSubCat;public string nameSubCat;public string idCat}
class ArticClass{public string idArt, public string name, public string idSubCat}
Get the Data and Fill the data to the respective class and form as List<category>, List<su.....,>
Then you can use the following code
TreeNode treeNodeParent = new TreeNode();
TreeNode treeNodeChd = new TreeNode();
foreach(CategoryClass cat in catList)
{
treeNodeChd = new TreeNode();
treeNodeChd=getsubDir(cat);
treeNodeParent.ChildNodes.Add(treeNodeChd);
}
TreeView1.Nodes.Add(treeNodeParent);
public TreeNode getSubCat(CategoryClass cat)
{
IEnumerable<subcatclass> subcatQuery= from subCa in subcatList where subCa.idCat=cat.idCat select subCa;
TreeNode par=new TreeNode();
TreeNode Chd=new TreeNode();
foreach(SubCatclass sub in subcatquery){
chd=new TreeNode();
chd=getArtl(sub);
chd.Text=sub.name;
par.ChildNodes.Add(chd);
}
return par;
}
public TreeNode getArtl(subcatClass cat)
{
IEnumerable<artlclass> artlQuery= from artl in ArtlList where artl.idCat=cat.idsubCat select artl;
TreeNode par=new TreeNode();
TreeNode Chd=new TreeNode();
foreach(ArtlClass art in artlQuery){
chd=new TreeNode();
chd.Text=art.name;
par.ChildNodes.Add(chd);
}
return par;
}
The above is the rough code I have return. This should be helpful for you.
这篇关于绑定treeView与递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!