Link to home
Start Free TrialLog in
Avatar of pbissegger
pbisseggerFlag 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 !
ASKER CERTIFIED SOLUTION
Avatar of guvera
guvera
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Victor Tarasov
paste
if not page.ispostback then
you code
end if
this will avoid problems with submit
Avatar of 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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok,

I have multilevel Treeview and I am loading data into it, and capturing the selections ! Wahoo !