从C#中的sql数据源填充Treeview [英] populating Treeview from sql datasource in C#

查看:119
本文介绍了从C#中的sql数据源填充Treeview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All



我正在尝试在c#中填充Treeview,它有三个级别。我有三个表,分类,商店和股票。表格引用如下;



股票

stockID

stockName

catID

storID



类别

catID

catName



商店

storID

storeNam



我运行sql存储过程使用以下代码检索股票详细信息;



选择a.stockID,a.stockName,b.catName,c.storeName

来自股票a

内连接类别b on a.catID = b.catID

内连接存储c on a.storeID = c.storeID



现在我想在这样的Treeview中显示查询结果;



+ storeName

..... + catName

.......... stockName



例子

+ Newyork商店

..... +水果

..........橙色



我怎样才能在c#中做到这一点,我到处都看,但我发现的只有g给我两个级别。



非常感谢提前。

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.

推荐答案

我一直被困在类似的问题,它可能不完全适合你的情况,但也许它可以让你深入了解你需要使用的循环实现(不限于级别数)



你能把对象作为分隔字符串(路径)吗?例如storeName.catName.stockName

如果是这样,您可以使用以下代码,或者至少使用循环实现,首先用您获得的每一行数据填充一个列表,然后将其传递给类似于方法。



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.

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);
		}
	}		
}


这篇关于从C#中的sql数据源填充Treeview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆