We help IT Professionals succeed at work.

Populating TreeView in VB.Net

pbissegger
pbissegger asked
on
756 Views
Last Modified: 2012-05-29
I am having a very difficult time finding anything that can help me populate a Treeview programmatically in VB.Net. Many articles use a "Set" which is no longer supported, or somehow don't address my simple needs.

Help !

Here is what I need

1. I have a 4 level list of a sports league:
     - 1st level : Leaguename
     - 2nd level: Team name
     - 3rd level: "Regular players" or "Spares"
     - 4th level : Player name

and I need name/value pairs stored in the tree

and I want to select the players using checkboxes

and I want to (on submit) get a list of the playerIDs which were selected.

2. I would like the code to look something like this:

      

' Add league node
myTreeView.Nodes.Add(New TreeNode(Leaguename, Session("Leaguenum")))


 ' Add teams
        cmd.CommandText = "select groupname, groupnum from leagues where leaguenum = " & Session("Leaguenum") & " and grouptype='Team' order by groupname"
        MyReader = cmd.ExecuteReader()
    
        Do While MyReader.Read()

        ' Add team mode
        myTreeView.Nodes(0).Nodes. Add(New TreeNode(Groupname, Groupnum))

        ' Add regular players
        cmd1.CommandText = "select l.groupnum as groupnum, c.lastname + ', ' + c.firstname as name, c.usernum as usernum from contacts c, leagues l where c.leaguenum = " & Session("Leaguenum") & " and l.groupnum = c.teamnum and playerstatus='regular'"
         MyReader1 = cmd1.ExecuteReader()  
        
        Do While MyReader1.Read()
    
        ' Add regular player node
        myTreeView.Nodes(0).Nodes(0).Nodes. Add(New TreeNode(name, usernum))
        
        Loop
    
        MyReader1.Close()
        
        ' Add spares
        cmd1.CommandText = "select l.groupnum as groupnum, c.lastname + ', ' + c.firstname as name, c.usernum as usernum from contacts c, leagues l where c.leaguenum = " & Session("Leaguenum") & " and l.groupnum = c.teamnum  and playerstatus='spare'"
        MyReader1 = cmd1.ExecuteReader()
    
        Do While MyReader1.Read()
    
       ' Add spare player node
        myTreeView.Nodes(0).Nodes(0).Nodes. Add(New TreeNode(name, usernum))
        Loop
    
        MyReader1.Close()

MyReader,Close()

Open in new window


with HTML code as:

 
<asp:TreeView ID="myTreeView" runat="server" ImageSet="Arrows" PathSeparator="|" ShowCheckBoxes="Leaf">
        <HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
        <SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD" HorizontalPadding="0px" VerticalPadding="0px" />
        <NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" HorizontalPadding="5px"  NodeSpacing="0px" VerticalPadding="0px" />
</asp:TreeView>

Open in new window


however the syntax I have for adding Nodes (and the Child nodes) is all wrong and I can't get it to work.

Help !
Comment
Watch Question

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Victor TarasovHead of system administration department
CERTIFIED EXPERT

Commented:
paste
if not page.ispostback then
you code
end if
this will avoid problems with submit

Author

Commented:
Guvera,

I tried out the first article - because it is VB and it makes a lot of sense - and made the following code:

        
Dim leaguenode As TreeNode
leaguenode = New TreeNode(Leaguename, Session("Leaguenum"))
myTreeView.Nodes.Add(leaguenode)
        
Dim teamnode As TreeNode
teamnode = New TreeNode()
teamnode = leaguenode.Nodes.Add(Adminname, "a1")

Open in new window


and I get a Compilation Error on the last line:

Compiler Error Message: BC30456: 'Nodes' is not a member of 'System.Web.UI.WebControls.TreeNode'.

I changed the code to:

       
 Dim leaguenode As TreeNode
        leaguenode = New TreeNode(Leaguename, Session("Leaguenum"))
        myTreeView.Nodes.Add(leaguenode)
        
        Dim teamnode As TreeNode
        teamnode = New TreeNode(Adminname, "a1")
        leaguenode.Nodes.Add(teamnode)

Open in new window


and I still get the same error on the 2nd to last line.

Any ideas ?

Thx, Pete
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Anuradha GoliSystems Development / Support Specialist
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Ok,

I have multilevel Treeview and I am loading data into it, and capturing the selections ! Wahoo !
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.