从数据库绑定树状视图中的数据 [英] To bind data in tree view from database

查看:68
本文介绍了从数据库绑定树状视图中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi

我在数据库中有一个名为menu的表,其中我有ID以及Parent menuid.并且ID为PK,并且会自动递增....

在数据库中,如果menu为父菜单,则parentID为零;如果为子菜单,则其parentid等于其父菜单的ID.

我想将TREEVIEW中的所有这些数据作为父菜单绑定为叶子中的根目录和子菜单

例如
项目(根)
-project1
-project2(root1)
-project2.1

project有子菜单project1和project2,而project2又有子菜单project2.1.

plz help.

hi

i have table called menu in database in which i have ID as well as Parent menuid. and ID is PK and it autoincrementes ....

inthe database if menu is parent menu its parentId is zero and if it is submenu then its parentid is equal to Id of its parent menu.

i want to bind all this data in the TREEVIEW as parent menu as root and submenu in the leaf

eg
Projects(root)
-project1
-project2(root1)
-project2.1

project has submenus project1 and project2 and again project2 has submenu project2.1.

plz help .

推荐答案

您不能直接将数据绑定到Treeview-它没有DataSource属性,因为它是一个分层的显示控件,并且数据库是平坦的数据容器.因此,如果您可以将数据库绑定到该数据库,则不会得到一棵树,而是会得到一个列表.由于这是对TreeView的不良使用,因此您不能这样做!

为了获得所需的结果,您需要使用DataReader,并自己添加TreeNode元素.这并不困难,只需要考虑一下.

1)遍历数据读取器.
2)如果父级为零,则将节点添加到根
3)在树中找到父级.
3.1)如果父节点不存在,则将节点添加到根目录(必须对此做一些操作-可以为其创建一个临时节点,但是这样就存在将其放置在何处的问题!) 3.2)如果有,请将新节点添加为子节点.
You can''t directly bind data to a Treeview - it has no DataSource property, because it is a hierarchical display control, and databases are flat data containers. So if you could bind a database to it, you would not get a tree - you would get a list. Since that is a bad use of a TreeView, you can''t do it!

To get the result you want, you need to use a DataReader, and add the TreeNode elements yourself. It''s not difficult, it just takes a little thinking about.

1) Loop through data reader.
2) If parent is zero, add node to root
3) Find parent in tree.
3.1) If the parent node doesn''t exist, add node to root (gotta do something with it - could create a temporary node for it, but then you have the problem of where to put that!)
3.2) If it does, add the new node as a child.


这篇关于从数据库绑定树状视图中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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