Click here to Skip to main content
15,888,020 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All

I am trying to populate a Treeview in c# which has three levels. I have three tables for Category, Store and Stock. the tables are referenced as follows;

Stock
stockID
stockName
catID
storID

Category
catID
catName

Store
storID
storeNam

I run an sql stored procedure with the following code to retrieve the stock details;

select a.stockID, a.stockName, b.catName, c.storeName
from Stock a
inner join Category b on a.catID = b.catID
inner join Store c on a.storeID = c.storeID

Now i want to desplay the result of the query in a Treeview like this;

+storeName
.....+catName
..........stockName

Example
+Newyork store
.....+Fruits
..........Orange

How can i do this in c#, i have looked everywhere but what i have found is only giving me two levels.

Many thanks in advance.
Posted
Comments
Bheeshm 1-Nov-13 8:00am    
Hi,

Please have a look @

http://aspdotnetcodebook.blogspot.dk/2008/11/sql-xml-output-to-populate-treeview.html
Kasekec 2-Nov-13 1:13am    
Thank you Bheeshm for your response, I am looking at your link now, I will report the outcome soon..

1 solution

I've been stuck on a similar problem, it might not completely fit your situation, but perhaps it can give you an insight on the loop implementation you need to use (not limited to the number of levels)

Can you get the objects as a delimited string (a "path")? For example storeName.catName.stockName
If so, you could use the follow code, or at least the loop implementation by first filling a list with each row of data you got, and then passing it through a sortlike method.

C#
public static void ListToTreeview(TreeView treeview, List ListOfRows)
{
	// Place the root node to work from in the treeview
	TreeNode root = new TreeNode(ListOfRows[0]);
	TreeNode node;

	treeview.Nodes.Add(root);

	// Loop through each row
	foreach(string s in ListOfRows)
	{
		node = root;
		foreach (string level in samenvoegveld.Split('.'))
		{
			node = AddNode(node, level);
		}
	}		
}
 
Share this answer
 
Comments
Kasekec 2-Nov-13 1:15am    
Thank you Milfje, I'll report back once I try your solution. Many thnx

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