?
Solved

Populate a treeview

Posted on 2012-09-12
4
Medium Priority
?
616 Views
Last Modified: 2012-09-16
Hi experts,
I've been tasked with populating a treeview within vb.net (windows application)

I've been given the SQL and can successfully connect to the DB and return the data into a datatable.

the data looks like this....


USER_ID,NAME,ROLE_ID,ROLE_DESC,ACTIVE_IND
1,ANDREW SIMPSON,1,Information,Y
1,ANDREW SIMPSON,2,Registry,Y
2,GEORGE Binns,1,Information,Y
2,GEORGE Binns,3,Charge,Y
5,PAUL BOWMAN,1,Information,Y
5,PAUL BOWMAN,2,Registry,Y
5,PAUL BOWMAN,3,Charge,Y
88,MICK JAGGER,1,Information,Y
88,MICK JAGGER,2,Registry,Y
88,MICK JAGGER,3,Charge,Y

However i'm struggling to populate a treeview it should look like

Name
   Role description
   Role description
Name
   Role description

So should look like
Andrew Simpson
   Information
   Registry
George Binns
   Information
   Charge
etc etc.....

Any help would be very useful as i've not used the treeview before and struggling at the moment
Thanks
Mark
0
Comment
Question by:potnoodle13
  • 2
4 Comments
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 38391447
Here is an example in C#.
http://www.dotnetperls.com/treeview

TreeView is a collection of nodes, so you would populate nodes and subnodes and then add them to the treeview.
0
 

Author Comment

by:potnoodle13
ID: 38391553
thank you for the quick response however as you said that is C# not VB.NET i've checked the link and it doesn't help as it doesn't have any database connection at all
0
 
LVL 20

Accepted Solution

by:
Daniel Van Der Werken earned 2000 total points
ID: 38392103
Sorry. I'm a C# dev and figure it's not too hard to translate. Here is a link using VB .NET
http://www.codeproject.com/Articles/23114/Working-with-TreeView-Controls

If you are reading from a database, you'd do something like this: (psuedocode because I don't really know VB .NET all that well):

while (myReader.Read() )
   begin
      Dim mynode As new TreeNode()
      mynode.Name = myReader("NAME")
      mynode.Text = myReader("NAME")
      Dim mySubNode As new TreeNode()
      mySubNode.Name = myReader("ROLE")
      mySubNode.Text = myReader("ROLE")
      mynode.Nodes.Add(mySubNode)
      treeView1.Nodes.Add(mynode)
   end

Open in new window

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38393032
Once you have the datatable, you can get the unique names into another table

assuming that dTable is the datatable

Dim dNames As DataTable = dTable.DefaultView.ToDataTable(true, "Name")
For i as integer = 0 to dNames.Rows.Count - 1
    MyTree.Nodes.Add(New TreeNode(dNames.Rows(i).Item("Name")))
Next

Then handle the BeforeExpand event of tree.

dTable.DefaultView.RowFilter = "Name='" & e.Node.Text & "'"
For i As Integer = 0 to dTable.DefaultView.Count - 1
      e.Node.Nodes.Add(New TreeNode(dTable.DefaultView.Item(i).Item("Role_Desc")))
Next
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question