Populate a treeview

Posted on 2012-09-12
Last Modified: 2012-09-16
Hi experts,
I've been tasked with populating a treeview within (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....

1,ANDREW SIMPSON,1,Information,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

   Role description
   Role description
   Role description

So should look like
Andrew Simpson
George Binns
etc etc.....

Any help would be very useful as i've not used the treeview before and struggling at the moment
Question by:potnoodle13
    LVL 19

    Expert Comment

    by:Daniel Van Der Werken
    Here is an example in C#.

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

    Author Comment

    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
    LVL 19

    Accepted Solution

    Sorry. I'm a C# dev and figure it's not too hard to translate. Here is a link using VB .NET

    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() )
          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")

    Open in new window

    LVL 83

    Expert Comment

    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")))

    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")))

    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now