Avatar of transstates
transstatesFlag for United States of America

asked on 

Load Treeview using Dataset

Good Morning All,

What am I doing wrong?  What I have is a dataset that has three fields (parent, pn, sn) and I want these to display in a treeview as follows:

        Parent
            pn
                 sn
                 sn
             pn
                  sn
                  sn

and so on.  What I get when I run my code is the parent multiple times.  I want 1 parent with ? of children with ? of sub children.  Heres a piece of code I'm using:
 
       tv.nodes.clear
       tv.BeginUpdate()
        Dim dt As DataTable = dsData.Tables("table1")
        For Each dr As DataRow In dt.Rows
           
            Dim tnRoot As TreeNode = New TreeNode(dr.Item("parent").ToString)
            tnRoot.Nodes.Add(dr.Item("pn").ToString)
            tnRoot.Nodes.Add(dr.Item("sn").ToString)
            tv.Nodes.Add(tnRoot)

        Next
        tv.EndUpdate()

This displays:

Parent
   -->pn
   -->sn(this is the subchild)

Parent (same as above)
   -->pn (same as above)
   -->sn (different)

Hope this makes sense?!?!

Thanks in advance...

jim
Visual Basic.NET

Avatar of undefined
Last Comment
Nasir Razzaq
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

I think you need to change this

        For Each dr As DataRow In dt.Rows
           
            Dim tnRoot As TreeNode = New TreeNode(dr.Item("parent").ToString)
            tnRoot.Nodes.Add(dr.Item("pn").ToString)
            tnRoot.Nodes.Add(dr.Item("sn").ToString)
            tv.Nodes.Add(tnRoot)

        Next

to

        For Each dr As DataRow In dt.Rows
           
            Dim tnRoot As TreeNode
            If tv.Nodes.ContainsKey(dr.Item("parent")) Then
               tnRoot  = tv.Nodes(dr.Item("parent"))
            Else
               tnRoot  = New TreeNode(dr.Item("parent").ToString)
            End If
            tnRoot.Nodes.Add(dr.Item("pn").ToString)
            tnRoot.Nodes.Add(dr.Item("sn").ToString)
            tv.Nodes.Add(tnRoot)

        Next
Avatar of transstates
transstates
Flag of United States of America image

ASKER

Thanks for the help CC:

Ok, in my dataset I have multiple parent number each with mulitiple pn but different sn.  So right now it displays like this:

1170AB (parent)
     1133 (pn)
      AB23 (sn)

1170AB (parent)
     1133 (pn)
      DEF12 (sn)

and so on.....

I'm trying for this:

1170AB (parent)
     1133 (pn)
           AB23 (sn)
           DEF12 (sn)

Hope this helps.. !!!  :)

jim
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Did you try my code?
Avatar of transstates
transstates
Flag of United States of America image

ASKER

Yes I did.  My last comment was the results.
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Visual Basic.NET
Visual Basic.NET

Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,

96K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo