Treeview problem

M

MaPet

He. 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
 
S

Steven Nagy

I would solve this by:

Filling a dataset with all employees.
Using RECURSION I would add an employee, and then iterate through the
dataset to find any one below him.

Rinse, lather, and repeat.
 
M

MaPet

Steven Nagy said:
I would solve this by:

Filling a dataset with all employees.
Using RECURSION I would add an employee, and then iterate through the
dataset to find any one below him.

Rinse, lather, and repeat.

can you write some example code, please.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top