Solved

Adding to the Treeview

Posted on 2011-03-19
6
918 Views
Last Modified: 2012-08-14
I'm writing a vb.net application that will use the treeview control to display info to the users.  It works to add a node to the first level nodes, but when trying to add a node to the second level nodes, it adds a node but it has no information assigned to it.  The way I add a node to the tree is I create a tree node, then assign information to it, then add it to the tree.  I've displayed a message box with information about the treenode and all information is there before its added.  My code basically works this way below:

Get info for node0

Treeview.nodes.add(node0)

Get info for node1

Treeview.nodes(node0).nodes.add(node1)

Get info for node2

Treeview.nodes(node0).nodes(node1).nodes.add(node2)

Node2 doesn't get added correctly. All information is correct in Node2 and something does get added to the tree but with no information.  Has anyone had this problem before?  Thanks for any help.
0
Comment
Question by:rcblevins
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 35171943
have you tried to keep a reference to the node you just added:

mynode0 = Treeview.nodes.add(node0)
mynode1 = mynode0.nodes.add(node1)
mynode1.nodes.add(node2)
0
 
LVL 12

Expert Comment

by:omegaomega
ID: 35172307
Hello, rcblevins,

I'm not sure what you mean by "information", but as far as I know Nodes requires an integer index, so an expression like:

    Treeview.nodes(node0).nodes.add(node1)

should cause an error.  If you can show the actual code that you are using to populate the TreeView we may be able to provide more help.

Cheers,
Randy


0
 

Author Comment

by:rcblevins
ID: 35172373
Here is the code.  The problem is where node2 is being used.  The stn node has information in it when its added to the tree, but the tree shows that a blank node is added.  The other wierd thing is that I also tried to show the name of the node refereced by node1 which should be node2's parent and it's name comes back empty.  Thanks for your help.

Dim node0 As Integer
        Dim node1 As Integer
        Dim node2 As Integer
        Dim node3 As Integer
        Dim node4 As Integer


        TreeView.Nodes.Clear()

        TreeView.BeginUpdate()

              Dim mddr As SqlDataReader

            mddr = Get Data

            If (mddr.HasRows) Then

                While (mddr.Read())


                    Dim mdtn As New TreeNode


                    mdtn.Tag = mddr.GetInt32(0)
                    mdtn.Text = mddr.GetString(1)
                    mdtn.ToolTipText = mddr.GetInt32(0).ToString

                    node0 = TreeView.Nodes.Add(mdtn)

                    Dim cdr = Get Data

                    If (cdr.HasRows) Then

                        While cdr.Read()

                            Dim ctn As New TreeNode

                            ctn.Tag = cdr.GetInt32(0)
                            ctn.Text = cdr.GetString(1)
                            ctn.ToolTipText = cdr.GetInt32(0).ToString


                            node1 = TreeView.Nodes(node0).Nodes.Add(ctn)


                                Dim sdr = Get Data

                                If (sdr.HasRows) Then

                                    While sdr.Read()

                                        Dim stn As New TreeNode

                                        stn.Tag = sdr.GetString(0)
                                        stn.Name = sdr.GetString(1)


                                        node2 = TreeView.Nodes(node0).Nodes(node1).Nodes.Add(stn)

                                   End While

                                End If

                                sdr.Close()

                            End If

                        End While

                    End If

                    cdr.Close()

                End While


            End If

            mddr.Close()


        End If

        TreeView.EndUpdate()
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 70

Expert Comment

by:Éric Moreau
ID: 35172452
>>node1 = TreeView.Nodes(node0).Nodes.Add(ctn)

node1 cannot be an integer. you have mixed your variables. see how I fill a treeview at http://www.emoreau.com/Entries/Articles/2006/04/The-Treeview-control.aspx
0
 
LVL 12

Accepted Solution

by:
omegaomega earned 500 total points
ID: 35172508
Hello, rcblevins,

I think that you just are missing a line that reads:

    stn.Text = ...

Cheers,
Randy
0
 

Author Comment

by:rcblevins
ID: 35172662
Lol!  I had looked and looked over that code.  I was thinking in the back of my mind that is was something simple.  I just needed someone else to see it I guess.  Thanks for the help.
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

688 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