473,396 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Using Treeview to build an employee hierarchy

petepell
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!

Thanks!
Peter

Expand|Select|Wrap|Line Numbers
  1. Public Function BuildHierarchy() As DataSet
  2.  
  3. 'Experimental code for the nested hierarchial view
  4.  
  5. Dim result As New DataSet
  6. Dim connString As String = "server = .\sqlexpress;integrated security = true;database = Northwind"
  7. Dim myConnection = New SqlConnection(connString)
  8.  
  9. If (True) Then
  10. Dim query As String = "Select EmployeeID,FirstName,LastName,Title,ReportsTo from Employees"
  11.  
  12. Dim myCommand As New SqlCommand(query, myConnection)
  13. Dim myAdapter As New SqlDataAdapter
  14.  
  15. myCommand.CommandType = CommandType.Text
  16. myAdapter.SelectCommand = myCommand
  17. myAdapter.Fill(result)
  18. myAdapter.Dispose()
  19.  
  20. End If
  21.  
  22. result.DataSetName = "Employees"
  23. result.Tables(0).TableName = "Employee" '
  24.  
  25. Dim relation As New DataRelation("ParentChild", result.Tables("Employee").Columns("EmployeeID"), result.Tables("Employee").Columns("ReportsTo"), True)
  26.  
  27. relation.Nested = True
  28. result.Relations.Add(relation)
  29. result.GetXml()
  30.  
  31. Dim nodeAssociate As TreeNode
  32. Dim nodeSubAssociate As TreeNode
  33.  
  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)
  38.  
  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
  45.  
  46. End Function
  47.  
Feb 13 '08 #1
0 1645

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

Similar topics

0
by: imassadpk | last post by:
Hi all, Apprecite your help in resolving this tricky issue... I am trying implementing Recurrsive TreeView in MS. Access 2003 ADP project. So far, no luck :( The Problem: This sample...
1
by: R. Sammut | last post by:
Hi, I have a treeview control that is updated every 30 seconds. The treeview nodes are cleared and new nodes (same number of nodes and hierarchy) are added to the treeview. Now I need to save...
3
by: Trond Hoiberg | last post by:
I have a lil problem i am struggling with. I have a folder LOGFILES that contains subfolder. Like this: MACHINE1 --DATE1 --DATE2 --DATE3 MACHINE2 --DATE1 --DATE2
0
by: Henry | last post by:
I have a dataset dsMain in which I have two tables The table period has columns like period_id, name, root_org_id.... The table organization has org_id, name, parent_id, hier_path, level,...
6
by: meh | last post by:
I can figure out the total number of nodes in a given tree but what I'd like to know is what is the Selected Nodes relationship to the entire tree i.e This is node n out of nnn nodes. In most of...
2
by: Eamon Millman | last post by:
Greetings, I'm fairly new to the new ASP.NET 2.0 controls and I'm trying to get the TreeView control to display some non-xml/sitemap data I want to have in a tree format. To accomplish this...
0
by: rajeshkj | last post by:
In my table three field is there:-EmpId,EmpName,Mngr_Id.Now I want to show the Employee name on node in TreeView(ASP.NET).In Root node only those employee who is not having any manager after that in...
0
by: mskhan | last post by:
HI Guys! I want to use asp.net 2.0 treeview control. Need to build deep hierarchy. I have a function that returns details of an employee such as Name, Dept_Own_id, designation, emplid etc, based on...
0
by: dutsnekcirf | last post by:
Okay, I think this one is a really hard one for yous geniuses out there. I'm going to try my best to explain in as much detail as I can. And for the most part I just want to know if I'm doing this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.