clintnash
asked on
TreeView - get all selected (checkbox) nodes on button click
I have a treeview control with checkboxes that I need to get the unique identifier value of all checked nodes on a button click. I dont need the control to update until the user actually clicks the button. A stored procedure generates a datatable with the following structure.
SportID SportName LeagueID League
-------------------------- ---------- ---------- ---------- ----------
101 Basketball 400 1st Grade
101 Basketball 401 2nd Grade
102 Volleyball 650 Girls Red Div
etc.
The Treeview is build the tree using the code below.
The Treeview displays SportName and League, but the value that I need to retrieve is the LeagueID. Once the user clicks the button, I need a list of all the checked LeagueIDs. In a datagrid I would do something like
For n as integer = 0 to Datagrid.Rows.Count -1
If Datagrid.Rows(n).Cells(5). Value = true then
LeagueID = Datagrid.Rows(n).Cells("Le agueID").v alue
stringbuilder or something with LeagueID
end if
Next.
But I can't seem to find a similar method to work with the treeview.
Thanks,
Clint...
SportID SportName LeagueID League
--------------------------
101 Basketball 400 1st Grade
101 Basketball 401 2nd Grade
102 Volleyball 650 Girls Red Div
etc.
The Treeview is build the tree using the code below.
The Treeview displays SportName and League, but the value that I need to retrieve is the LeagueID. Once the user clicks the button, I need a list of all the checked LeagueIDs. In a datagrid I would do something like
For n as integer = 0 to Datagrid.Rows.Count -1
If Datagrid.Rows(n).Cells(5).
LeagueID = Datagrid.Rows(n).Cells("Le
stringbuilder or something with LeagueID
end if
Next.
But I can't seem to find a similar method to work with the treeview.
Thanks,
Clint...
ASKER
Well I am able to pull the text value from the node, but what I need is the uniqueID for the node. I am not sure why I can't see the code that inserted to show how I am building the tree, so if you are seeing this twice sorry. I pass a datatable to the following sub.
Private Sub BuildTree(ByVal DT As DataTable)
TreeView1.Nodes.Clear()
Dim root As New TreeNode("Sport")
Dim sportNodes() As TreeNode
Dim sportNode As TreeNode
For Each Dr As DataRow In DT.Rows
sportNodes = root.Nodes.Find(Dr("SportI D"), True)
If sportNodes IsNot Nothing And sportNodes.Length > 0 Then
sportNode = sportNodes(0)
Else
sportNode = root.Nodes.Add(Dr("SportID "), Dr("Sportname"))
End If
sportNode.Nodes.Add(Dr("Le agueID"), Dr("League"))
Next
TreeView1.Nodes.Add(root)
End Sub
Using the code above I am able to sucessfully fill the list with "League", what I need is "LeagueID". Hopefully that is an easy change, but I have spent the afternoon trying to work it out without success.
thanks,
Clint...
Private Sub BuildTree(ByVal DT As DataTable)
TreeView1.Nodes.Clear()
Dim root As New TreeNode("Sport")
Dim sportNodes() As TreeNode
Dim sportNode As TreeNode
For Each Dr As DataRow In DT.Rows
sportNodes = root.Nodes.Find(Dr("SportI
If sportNodes IsNot Nothing And sportNodes.Length > 0 Then
sportNode = sportNodes(0)
Else
sportNode = root.Nodes.Add(Dr("SportID
End If
sportNode.Nodes.Add(Dr("Le
Next
TreeView1.Nodes.Add(root)
End Sub
Using the code above I am able to sucessfully fill the list with "League", what I need is "LeagueID". Hopefully that is an easy change, but I have spent the afternoon trying to work it out without success.
thanks,
Clint...
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Fantastic, made it quite a step further today, I do still have one final issue. The code as it is above didn't work when I cut it in, I made the following change to it and all seems okay now.
sportNode.Nodes.Add(Dr("Le agueID"), Dr("League")) .Tag=(Dr("LeagueID").ToStr ing)
The problem that I cant seem to get past is, it works just fine as long as I only select the child nodes (League / LeagueID), if I select its parent (Sport / SportID), it errors with a Object Reference not set to an instance of an object.
I do, very much, appreciate your help!
sportNode.Nodes.Add(Dr("Le
The problem that I cant seem to get past is, it works just fine as long as I only select the child nodes (League / LeagueID), if I select its parent (Sport / SportID), it errors with a Object Reference not set to an instance of an object.
I do, very much, appreciate your help!
hmmm... where is it erroring out? on the line where we add the tag to the list?
ASKER
Actually, its was a bit of the forest for the trees problem, the error was being generated when I was looping through the list and using stringbuilder to construct a comma seperated string of the selected items. When I saw your post on where it was erroring out, it clicked. I put in a "isnot nothing" check on list.item(n) as I loop through and its working perfectly now.
thanks again for all your help...
thanks again for all your help...
Great! Glad we got it working.
Open in new window