Click here to Skip to main content
15,896,727 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
XML
Category (idCategory, nameCategory)
subCategory (idSubCategory, nameSubCategorie, idCategory)
Articles (idArticle, nameArticle, idSubCategory)

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:
XML
+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

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.
Posted
Updated 21-May-13 19:29pm
v6
Comments
Sunasara Imdadhusen 21-May-13 3:39am    
You have to use recursion to bind tree

1 solution

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.
 
Share this answer
 
Comments
Nnorss 22-May-13 5:21am    
Thank you a lot Prasaad! The code "par.ChildNodes" returns an error: System.Windows.forms.Treenode doesn't contains any definition and any extension method 'ChildNodes'... a directive using is missing?... I'm on framework 4.0, do you have any ideas of how to fix this please?
Prasaad SJ 22-May-13 6:04am    
Hi.. Nnorss.. ChildNodes is for the Webforms.
If you are using Windows Forms then use par.Nodes.Add(childnode);
Nnorss 22-May-13 6:12am    
Ok! Just one more thing please, I usually use LINQ to entities to interact with my database... How should I fill the data to the respective class and form as List..?
Prasaad SJ 22-May-13 21:15pm    
Hi.. It is not necessary that you have to use Class for that. You can query the data with the dataset itself.Following link will be helpful for you

http://msdn.microsoft.com/en-us/library/bb386910.aspx
Nnorss 24-May-13 18:58pm    
I used my database enities contex instead of dataset and it worked very fine, thank you very much Prassad!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900