M
MaPet
I'm trying to create a treeview that will show data from access database
in hierarchical way. Database structure is: ID, FirstName, LastName,
IDBoss - IDBoss is referenced to ID. Treeview should look like this:
-employee1
---employee2
---employee3
------employee4
---employee5
---employee6
------employee7
---------employee8
------employee9
Can someone please send here some code (please in vb.net 2005) that will do
that work, or give me a hint. So far I create function that populate
treeview but only 3 levels.
Here is my code:
(Sorry on my english)
----
Private Sub FillTree()
Dim conn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=|DataDirectory|\baza.mdb")
Dim rootAdapter As New OleDb.OleDbDataAdapter("SELECT * FROM zaposlenici
WHERE ID = 1", conn)
Dim childAdapter As New OleDb.OleDbDataAdapter("SELECT * FROM zaposlenici
WHERE ID <> 1", conn)
Dim treeDataSet As New DataSet()
treeDataSet.EnforceConstraints = False
rootAdapter.Fill(treeDataSet, "Parents")
childAdapter.Fill(treeDataSet, "Children")
treeDataSet.Relations.Add("relacija",
treeDataSet.Tables("Parents").Columns("ID"), _
treeDataSet.Tables("Children").Columns("IDBoss"))
treeDataSet.Relations.Add("relacija2", _
treeDataSet.Tables("Children").Columns("ID"), _
treeDataSet.Tables("Children").Columns("IDBoss"))
TreeView1.Nodes.Clear()
Dim parent As DataRow
For Each parent In treeDataSet.Tables("Parents").Rows
Dim parentNode As New TreeNode(parent("LastName").ToString() & " " &
parent("FirstName").ToString())
Dim child As DataRow
For Each child In parent.GetChildRows("relacija")
Dim folderNode As New TreeNode(child("LastName").ToString() & " " &
child("FirstName").ToString())
Dim item As DataRow
For Each item In child.GetChildRows("relacija2")
folderNode.Nodes.Add(New TreeNode(item("LastName").ToString() & " " &
item("FirstName").ToString()))
Next item
parentNode.Nodes.Add(folderNode)
Next child
TreeView1.Nodes.Add(parentNode)
Next parent
TreeView1.ExpandAll()
End Sub
in hierarchical way. Database structure is: ID, FirstName, LastName,
IDBoss - IDBoss is referenced to ID. Treeview should look like this:
-employee1
---employee2
---employee3
------employee4
---employee5
---employee6
------employee7
---------employee8
------employee9
Can someone please send here some code (please in vb.net 2005) that will do
that work, or give me a hint. So far I create function that populate
treeview but only 3 levels.
Here is my code:
(Sorry on my english)
----
Private Sub FillTree()
Dim conn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=|DataDirectory|\baza.mdb")
Dim rootAdapter As New OleDb.OleDbDataAdapter("SELECT * FROM zaposlenici
WHERE ID = 1", conn)
Dim childAdapter As New OleDb.OleDbDataAdapter("SELECT * FROM zaposlenici
WHERE ID <> 1", conn)
Dim treeDataSet As New DataSet()
treeDataSet.EnforceConstraints = False
rootAdapter.Fill(treeDataSet, "Parents")
childAdapter.Fill(treeDataSet, "Children")
treeDataSet.Relations.Add("relacija",
treeDataSet.Tables("Parents").Columns("ID"), _
treeDataSet.Tables("Children").Columns("IDBoss"))
treeDataSet.Relations.Add("relacija2", _
treeDataSet.Tables("Children").Columns("ID"), _
treeDataSet.Tables("Children").Columns("IDBoss"))
TreeView1.Nodes.Clear()
Dim parent As DataRow
For Each parent In treeDataSet.Tables("Parents").Rows
Dim parentNode As New TreeNode(parent("LastName").ToString() & " " &
parent("FirstName").ToString())
Dim child As DataRow
For Each child In parent.GetChildRows("relacija")
Dim folderNode As New TreeNode(child("LastName").ToString() & " " &
child("FirstName").ToString())
Dim item As DataRow
For Each item In child.GetChildRows("relacija2")
folderNode.Nodes.Add(New TreeNode(item("LastName").ToString() & " " &
item("FirstName").ToString()))
Next item
parentNode.Nodes.Add(folderNode)
Next child
TreeView1.Nodes.Add(parentNode)
Next parent
TreeView1.ExpandAll()
End Sub