Vsiat,
In addition to the others comments, here is a Routine I put together a
couple of weeks ago:
Something like (for the SQL Server Northwind database, the Employees table).
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim data As DataSet = BuildEmployees()
Dim connection As New SqlClient.SqlConnection("packet
size=4096;integrated security=SSPI;data source=.;persist security
info=False;initial catalog=Northwind")
Dim adapter As New SqlClient.SqlDataAdapter("Select EmployeeID,
LastName, FirstName, ReportsTo from Employees", connection)
adapter.Fill(data, "Employees")
For Each row As DataRow In
data.Tables("Employees").Select("ReportsTo is Null")
AddChild(TreeView1.Nodes, row)
Next
End Sub
Private Sub AddChild(ByVal nodes As TreeNodeCollection, ByVal row As
DataRow)
Const format As String = "{1}, {0}"
Dim node As New TreeNode(String.Format(format, row("FirstName"),
row("LastName")))
nodes.Add(node)
AddChildren(node, row)
End Sub
Private Sub AddChildren(ByVal parentNode As TreeNode, ByVal parentRow As
DataRow)
For Each childRow As DataRow In
parentRow.GetChildRows("EmployeesEmployees")
AddChild(parentNode.Nodes, childRow)
Next
End Sub
Private Function BuildEmployees() As DataSet
Dim data As New DataSet
Dim table As New DataTable("Employees")
Dim columnEmployeeID As New DataColumn("EmployeeID",
GetType(Integer))
table.Columns.Add(columnEmployeeID)
Dim columnLastName As New DataColumn("LastName", GetType(String))
table.Columns.Add(columnLastName)
Dim columnFirstName As New DataColumn("FirstName", GetType(String))
table.Columns.Add(columnFirstName)
Dim columnReportsTo As New DataColumn("ReportsTo", GetType(Integer))
table.Columns.Add(columnReportsTo)
table.PrimaryKey = New DataColumn() {columnEmployeeID}
data.Tables.Add(table)
data.Relations.Add("EmployeesEmployees", columnEmployeeID,
columnReportsTo)
Return data
End Function
Instead of building the table inline as I have, you could use a typed
dataset.
The important things in this routine are:
1. The primary key on the Employees data table.
2. The relationship between the EmployeeID column & the ReportsTo column of
the Employees data table.
3. The GetChildRows on the relationship defined in item 2.
An alternative is to only bring back child nodes when that node is expanded,
which has a significantly better startup, but the initial expanding of each
node is slightly slower. Unfortunately I do not have a full sample of this
alternative handy.
Hope this helps
Jay
vsiat said:
I am trying to create a treeview out of a database table
with the typical structure ID, NAME, PARENTID, TYPE,
EXTRA_INFO, where [parentid] is linked to the [id].
What I want to achieve is create a tree made of custom,
extended nodes, which include all the extra information
contained in the table and not just typical TreeNode
objects.
To do that, I first created a structure with all the extra
information about the node (taken from the table), and
then created my own treenode class, adding the structure
as an extra property.
I am now trying to populate the treeview with my
treenodes, but that doesn't seem to work.
Any ideas or links to examples would be much appreciated.