Avatar of pbissegger
pbissegger
Flag for Canada asked on

Populating TreeView in VB.Net

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 !
ASP.NETVisual Basic.NET

Avatar of undefined
Last Comment
pbissegger

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
guvera

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Victor Tarasov

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

ASKER
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
SOLUTION
Nasir Razzaq

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
pbissegger

ASKER
Ok,

I have multilevel Treeview and I am loading data into it, and capturing the selections ! Wahoo !
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy