Treeview噩梦-如何读取数据. [英] Treeview nightmares - How to read data in.
问题描述
HI全部
我大部分时间都在看各种教程和示例,这些示例和示例说明了如何从数据读取器读取数据并填充树形视图.我已经研究过很多不同的方法(没有一个可以上班),以至于我已经放弃了我刚开始做的那一部分,所以我希望有人能找到一个简单的解决方案.
我有一个名为tbl_productcategories的mysql表,其中包含以下字段:
idtbl_productcategories(自动递增/唯一)
customer_id(INT)
parent_id(INT)
category_description(VARCHAR)
多个客户将使用同一应用程序,并且由于我们只希望树视图显示特定于用户的类别,因此将解释对customer_id字段的需求.
树状视图将显示不确定数量的子级,由于我今天查看的大多数代码都依赖于最多两个级别,因此您可能可以理解我的无奈.
从理论上讲,如果我对idtbl_productcategories进行排序,那么我永远不会遇到在添加父项之前尝试添加子项的情况.我的逻辑告诉我,如果节点索引等于表中自动编号字段中的值,那么我应该可以轻松地向该节点添加子节点.
拜托...我的逻辑哪里出问题了,我该怎么做?
谢谢
史蒂夫
HI All
I have spent most of the day looking at various tutorials and examples of how to read data in from a datareader and populate a treeview. I have looked at so many different ways of doing it (none that I can get to work) that I have forgoten the piece that I started with so I hope somebody out their has an easy solution.
I have a single mysql table called tbl_productcategories containing the following fields:
idtbl_productcategories (Auto Increment / unique)
customer_id (INT)
parent_id (INT)
category_description (VARCHAR)
The same application will be used by multiple customers and since we only want the treeview to display the categories specific to the user this will explain the need for the customer_id field.
The treeview is to display an indeterminate number of levels of children and since most of the code I have been viewing today all relies on a maximum of two levels you can probably understand my frustration.
In theory, if I sort on the idtbl_productcategories I will never have a situation where I attempt to add a child before the parent is added. My logic tells me that if I have a node index equal to the value in the autonumber field in the table then I should easily be able to add a child to that node.
Please guys...what is wrong with my logic and how do I do this?
Thanks
Steve
Dim con As New MySql.Data.MySqlClient.MySqlConnection("My Connection String goes here")
Dim cmd As New MySql.Data.MySqlClient.MySqlCommand("Select * FROM tbl_productcategories where customer_id = 1 order by idtbl_productcategories;")
Dim da As New MySql.Data.MySqlClient.MySqlDataAdapter
Dim ds As New DataSet
Dim dr As MySql.Data.MySqlClient.MySqlDataReader
cmd.Connection = con
If con.State <> ConnectionState.Open Then con.Open()
dr = cmd.ExecuteReader
While dr.read
'*****THIS IS THE PIECE THAT I HAVE NO IDEA WHAT TO DO*******
End While
con.Close()
DR.Close()
cmd.Dispose()
推荐答案
请参阅本文:
deepak-sharma.net/2012/02/08/how-to-bind-multiple-sql-server-tables-with-treeview-in-hierarchical-order
Refer to this article:
deepak-sharma.net/2012/02/08/how-to-bind-multiple-sql-server-tables-with-a-treeview-in-a-hierarchical-order
我假设使用WinForms;如果您的表单上有一个名为TreeView1
的TreeView
,则下面的代码读取并填充树状视图(我这里没有mysql,所以我只是为普通的sql读者准备的,但是您应该能够改变它);
I''m assuming WinForms; if you have a form with aTreeView
calledTreeView1
on it then the following code reads and populates the tree view (I haven''t got mysql here so I''ve just gone for normal sql readers but you should be able to change that);
Imports System.Data.SqlClient
Public Class Form1
Private Class ProductCategory
Public Property Id As Int32
Public Property CustomerId As Int32
Public Property ParentId As Int32
Public Property Description As String
Public Sub New(ByVal id As Int32, ByVal customerId As Int16, ByVal parentId As Int32, ByVal description As String)
Me.Id = id
Me.CustomerId = customerId
Me.ParentId = parentId
Me.Description = description
End Sub
End Class
Private Function FindParent(ByVal pc As ProductCategory, ByVal node As TreeNode) As TreeNode
Dim tag As ProductCategory = CType(node.Tag, ProductCategory)
If tag.Id = pc.ParentId Then Return node
For Each child As TreeNode In node.Nodes
Dim parent As TreeNode = FindParent(pc, child)
If Not IsNothing(parent) Then Return parent
Next
Return Nothing
End Function
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Using connection As SqlConnection = New SqlConnection("some super secret connection string")
connection.Open()
Using command As SqlCommand = New SqlCommand("select * from tbl_productcategories where customer_id=@customerId", connection)
command.Parameters.Add(New SqlParameter("customerId", 1))
Dim items As IList(Of ProductCategory) = New List(Of ProductCategory)
Using reader As SqlDataReader = command.ExecuteReader()
While reader.Read()
Dim id As Int32 = CType(reader("idtbl_productcategories"), Int32)
Dim customerId As Int32 = CType(reader("customer_id"), Int32)
Dim parentId As Int32 = 0
If Not IsDBNull(reader("parent_id")) Then
parentId = CType(reader("parent_id"), Int32)
End If
Dim description As String = CType(reader("category_description"), String)
items.Add(New ProductCategory(id, customerId, parentId, description))
End While
End Using
Dim rootNode As TreeNode = New TreeNode("Products")
rootNode.Tag = New ProductCategory(0, 0, 0, "Products")
For Each product As ProductCategory In items.OrderBy(Function(pc) pc.ParentId)
Dim node As TreeNode = New TreeNode(product.Description)
node.Tag = product
Dim parentNode As TreeNode = FindParent(product, rootNode)
parentNode.Nodes.Add(node)
Next
TreeView1.Nodes.Add(rootNode)
End Using
End Using
End Sub
End Class
希望这会有所帮助,
弗雷德里克
Hope this helps,
Fredrik
弗雷德里克....你是绝对的明星-谢谢!! :)
Fredrik....You are an absolute star - THANK YOU!! :)
这篇关于Treeview噩梦-如何读取数据.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!