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!
Thanks!
Peter
Expand|Select|Wrap|Line Numbers
- 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
- myAdapter.Fill(result)
- myAdapter.Dispose()
- 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
- result.Relations.Add(relation)
- result.GetXml()
- 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")
- tvwHierarchy.Nodes.Add(nodeAssociate)
- For Each rowTitle In rowAssociate.GetChildRows("ParentChild")
- nodeSubAssociate = New TreeNode()
- nodeSubAssociate.Text = rowTitle("EmployeeID") & ". " & rowTitle("FirstName") & " " & rowTitle("LastName")
- nodeAssociate.Nodes.Add(nodeSubAssociate)
- Next
- Next
- End Function