469,159 Members | 1,504 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,159 developers. It's quick & easy.

Nested hierarchial data in treeview

Hello all,

I am developing an application in VB 2008 that works with a SQL2005 DB to store and manipulate employee data. In one section of the app I want to be able to show a treeview of the hierarchy as a whole showing a nested hierarchy of who reports to who such that when it is first loaded, all you will see is the top guy in the company with a plus next to his name. Opening that will reveal his first level reports and opening those will reveal the next level and so on. This one piece of the puzzle has been the elemental drive of this application and it has yet to be accomplished.

The fields in the database that I need to work on are as such:

1. AssociateNumber (Basically the employee number of the person. Formatted like A-232)

2. Sponsor (This is the field showing the AssociateNumber of the person this associate reports to. It is also formatted like A-232)

Thus if A-20 reported to A-2 then it would look like this in A-20's row

AssociateNumber=A-20, Sponsor=A-2

I understand that I need to query the database and build a recordset for the data that I want to display and then populate the treeview from that recordset but I have been horribly unsuccessful thusfar.

Mostly I seem to be able to get the recordset created (using the Northwind as a test DB pulling from EmployeeID and ReportsTo fields) but when I create the treeview it seems to put all 9 of the employees on the first level as well as nesting them causing duplication of names.

Below is the code I used to accomplish this disaster.

Any help would be greatly appreciated!



Public Function BuildHierarchy() As DataSet

'Experimental code for the nested hierarchial view

Dim result As New DataSet
Dim connString As String = "server = .\sqlexpress;integrated security = true;database = Northwind"
Dim myConnection = New SqlConnection(connString)

If (True) Then
Dim query As String = "Select EmployeeID,FirstName,LastName,Title,ReportsTo from Employees"

Dim myCommand As New SqlCommand(query, myConnection)
Dim myAdapter As New SqlDataAdapter

myCommand.CommandType = CommandType.Text
myAdapter.SelectCommand = myCommand

End If

result.DataSetName = "Employees"
result.Tables(0).TableName = "Employee" '

Dim relation As New DataRelation("ParentChild", result.Tables("Employee").Columns("EmployeeID"), result.Tables("Employee").Columns("ReportsTo"), True)

relation.Nested = True

Dim nodeAssociate As TreeNode
Dim nodeSubAssociate As TreeNode

For Each rowAssociate In result.Tables("Employee").Rows
nodeAssociate = New TreeNode()
nodeAssociate.Text = rowAssociate("EmployeeID") & ". " & rowAssociate("FirstName") & " " & rowAssociate("LastName")

For Each rowTitle In rowAssociate.GetChildRows("ParentChild")
nodeSubAssociate = New TreeNode()
nodeSubAssociate.Text = rowTitle("EmployeeID") & ". " & rowTitle("FirstName") & " " & rowTitle("LastName")

End Function
Feb 8 '08 #1
0 1037

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by rawCoder | last post: by
2 posts views Thread by Franz | last post: by
2 posts views Thread by Rythm Music | last post: by
6 posts views Thread by Susan H | last post: by
reply views Thread by Henry | last post: by
5 posts views Thread by Jake K | last post: by
reply views Thread by Benny J | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.