473,789 Members | 2,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using Treeview to build an employee hierarchy

petepell
3 New Member
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 1665

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

Similar topics

0
2092
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 snippet is all written for DAO and doesn't work in ADO
1
6166
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 the state of the expanded treeview so that I can apply the same expanded state to the updated treeview. is there something i can do?
3
2048
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
1253
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, org_index, and period_id. There is a one to many relationship between period and organization, but the organization is actually a hierarchy. period.root_or_id represents the org_id of the root node of the organization hierarchy and that organization...
6
14209
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 my database apps its common place to tell the user that they are working on record x of xxx records I'm trying to provide the same functionality using the tree control. TIA meh
2
3818
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 I have followed the sample code provided on the MSDN website for writing a custom HierarchicalDataSourceControl, HierarchicalDataSourceView, IHierarchicalEnumerable, and IHierarchyData implementations (their FileSystem example).
0
1146
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 second level only those employee who are under manager but they have subordinate and after that in third level of child node respective subordinate should be there.I am not able to write query for second level employee who is having subordinate.That...
0
764
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 the deptid. The Deptid that we pass in this fucntion return the records of the employee under that dept. So i want to call the same fucntion recursivley, add the nodes to the tree and build a tree.
0
2211
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 the right way of it there's an easier way to do it. Background/Platform: Windows XP Office Excel 2007 Visual Studio Tools for Office 2008 Document level customization VB.Net I have a spreadsheet that is used to project which site(s) a...
0
9506
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10403
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10193
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10136
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9978
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6755
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4087
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.