Solved

TreeView - get all selected (checkbox) nodes on button click

Posted on 2008-06-17
7
9,481 Views
Last Modified: 2013-11-05
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("LeagueID").value
               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...

     
0
Comment
Question by:clintnash
  • 4
  • 3
7 Comments
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 21806252
Basically the same idea. Iterate through the list of nodes and get the keys for all selected nodes. The routine below uses recursion to add all checked nodes to a list; you could create a list for keys only quite easily.
(use)	Dim list As New List(Of TreeNode)

	GetSelectedNodes(treeView.Nodes, list)
 

Private Sub GetSelectedNodes(ByVal nodes As TreeNodeCollection, ByRef list As List(Of TreeNode))

	For Each node As TreeNode In nodes

		If node.Checked Then list.Add(node)

		GetSelectedNodes(node.Nodes, list)

	Next

End Sub

Open in new window

0
 
LVL 1

Author Comment

by:clintnash
ID: 21807988
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("SportID"), 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("LeagueID"), 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...
0
 
LVL 24

Accepted Solution

by:
Jeff Certain earned 500 total points
ID: 21808070
I'd change your code just a little bit to use the Tag property, since it seems that the key isn't recoverable (for some inexplicable reason).


  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("SportID"), 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("LeagueID"), Dr("League")).Tag(Dr("LeagueID").ToString)

        Next

        TreeView1.Nodes.Add(root)

    End Sub
 

(use)		Dim list As New List(Of String)

		GetSelectedNodes(treeView.Nodes, list)
 

	Private Sub GetSelectedNodes(ByVal nodes As TreeNodeCollection, ByRef list As List(Of String))

		For Each node As TreeNode In nodes

			If node.Checked Then list.Add(node.Tag)

			GetSelectedNodes(node.Nodes, list)

		Next

	End Sub

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:clintnash
ID: 21818377
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("LeagueID"), Dr("League")) .Tag=(Dr("LeagueID").ToString)

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!


0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 21818778
hmmm... where is it erroring out? on the line where we add the tag to the list?
0
 
LVL 1

Author Comment

by:clintnash
ID: 21823088
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...
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 21823226
Great! Glad we got it working.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now