By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,876 Members | 1,227 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,876 IT Pros & Developers. It's quick & easy.

Using Treeview to build an employee hierarchy

P: 3
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!


Expand|Select|Wrap|Line Numbers
  1. Public Function BuildHierarchy() As DataSet
  3. 'Experimental code for the nested hierarchial view
  5. Dim result As New DataSet
  6. Dim connString As String = "server = .\sqlexpress;integrated security = true;database = Northwind"
  7. Dim myConnection = New SqlConnection(connString)
  9. If (True) Then
  10. Dim query As String = "Select EmployeeID,FirstName,LastName,Title,ReportsTo from Employees"
  12. Dim myCommand As New SqlCommand(query, myConnection)
  13. Dim myAdapter As New SqlDataAdapter
  15. myCommand.CommandType = CommandType.Text
  16. myAdapter.SelectCommand = myCommand
  17. myAdapter.Fill(result)
  18. myAdapter.Dispose()
  20. End If
  22. result.DataSetName = "Employees"
  23. result.Tables(0).TableName = "Employee" '
  25. Dim relation As New DataRelation("ParentChild", result.Tables("Employee").Columns("EmployeeID"), result.Tables("Employee").Columns("ReportsTo"), True)
  27. relation.Nested = True
  28. result.Relations.Add(relation)
  29. result.GetXml()
  31. Dim nodeAssociate As TreeNode
  32. Dim nodeSubAssociate As TreeNode
  34. For Each rowAssociate In result.Tables("Employee").Rows
  35. nodeAssociate = New TreeNode()
  36. nodeAssociate.Text = rowAssociate("EmployeeID") & ". " & rowAssociate("FirstName") & " " & rowAssociate("LastName")
  37. tvwHierarchy.Nodes.Add(nodeAssociate)
  39. For Each rowTitle In rowAssociate.GetChildRows("ParentChild")
  40. nodeSubAssociate = New TreeNode()
  41. nodeSubAssociate.Text = rowTitle("EmployeeID") & ". " & rowTitle("FirstName") & " " & rowTitle("LastName")
  42. nodeAssociate.Nodes.Add(nodeSubAssociate)
  43. Next
  44. Next
  46. End Function
Feb 13 '08 #1
Share this question for a faster answer!
Share on Google+

Post your reply

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