How to check Checkbox in TreeView porgramatically

alsam
alsam used Ask the Experts™
on
Hi,
please can someone provide me the with the way how can I check checkbox for childnode in my Treeview after If condition is true. Code below

Thank you.
Kind regards.
Private Sub Show_filters(ByVal param As String, ByVal childnode As String)
        Dim cmdSelectFilters As New SqlCommand()
        Dim DataSetFilters As DataSet
        Dim Con As New SqlClient.SqlConnection(My.Settings.MyConnectionString)
        Dim DAFilters As New SqlClient.SqlDataAdapter
        Dim oDataRow As DataRow
        Dim oNode As New TreeNode
        Dim oTreeView As TreeView
        oTreeView = Me.TreeView1


        DAFilters.SelectCommand = cmdSelectFilters
        DAFilters.SelectCommand.Connection = Con
        DAFilters.SelectCommand.CommandText = "SELECT * FROM ALL_TREE_FILTERS WHERE LEAF_NAME = @param AND FILTER_NAME = @param2"
        DAFilters.SelectCommand.Parameters.Add("@param", SqlDbType.NVarChar)
        DAFilters.SelectCommand.Parameters("@param").Value = param
        DAFilters.SelectCommand.Parameters.Add("@param2", SqlDbType.NVarChar)
        DAFilters.SelectCommand.Parameters("@param2").Value = Me.ComboBox2.Text

        DataSetFilters = New DataSet()
        Con.Open()
        DAFilters.Fill(DataSetFilters, "All_Tree_Filters")
        Con.Close()


        For Each oDataRow In DataSetFilters.Tables("All_Tree_Filters").Rows

            If oDataRow("LEAF_VALUE") & (" " + oDataRow("DESCRIPTION").ToString()) = childnode Then

                '''' HERE I WANT TO SET CHECKBOX CHECKED FOR THE CHILDNODE''''

            End If
        Next oDataRow



    End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2015

Commented:

Commented:
Hello alsam,

Try the attached code.

To use the FindNode method, you just have to specify the name of the node you´re looking for and the root node to search. The function will return the first occurrency of node with the specified text.
Function FindNode(ByVal Text As String, ByVal Node As TreeNode) As TreeNode
        Dim n As TreeNode = Nothing
        If Node.Text = Text Then
            n = Node
        Else
            If Node.Nodes.Count > 0 Then
                For Each nd As TreeNode In Node.Nodes
                    If n IsNot Nothing Then Exit For
                    n = FindNode(Text, nd)
                Next
            End If
        End If
        Return n
    End Function

Private Sub Show_filters(ByVal param As String, ByVal childnode As String)
        Dim cmdSelectFilters As New SqlCommand()
        Dim DataSetFilters As DataSet
        Dim Con As New SqlClient.SqlConnection(My.Settings.MyConnectionString)
        Dim DAFilters As New SqlClient.SqlDataAdapter
        Dim oDataRow As DataRow
        Dim oNode As New TreeNode
        Dim oTreeView As TreeView
        oTreeView = Me.TreeView1


        DAFilters.SelectCommand = cmdSelectFilters
        DAFilters.SelectCommand.Connection = Con
        DAFilters.SelectCommand.CommandText = "SELECT * FROM ALL_TREE_FILTERS WHERE LEAF_NAME = @param AND FILTER_NAME = @param2"
        DAFilters.SelectCommand.Parameters.Add("@param", SqlDbType.NVarChar)
        DAFilters.SelectCommand.Parameters("@param").Value = param
        DAFilters.SelectCommand.Parameters.Add("@param2", SqlDbType.NVarChar)
        DAFilters.SelectCommand.Parameters("@param2").Value = Me.ComboBox2.Text

        DataSetFilters = New DataSet()
        Con.Open()
        DAFilters.Fill(DataSetFilters, "All_Tree_Filters")
        Con.Close()


        For Each oDataRow In DataSetFilters.Tables("All_Tree_Filters").Rows

            If oDataRow("LEAF_VALUE") & (" " + oDataRow("DESCRIPTION").ToString()) = childnode Then
               Dim node As TreeNode = FindNode(childnode, oTreeView.Nodes(0))
               node.Checked = True
            End If
        Next oDataRow



    End Sub

Open in new window

Author

Commented:
Hello 1303gun,
Thanks for provided solution...Unfortunatelly execution breaks in line node.Checked = True
It says "Object reference not set to an instance of an object.". Any idea?
Regards

Commented:
Hello alsam. That happens when the FindNode does not actually find any nodes with the specified text. Well, let me see if I understood the code. Does the childnode variable store the text in the node?

Author

Commented:
Hello 1303gun,
I will try to simplify to you by providing the part of code from where I actually pass the childnode variable. As you can see I forward oNode.Text...
I hope this is good enough for you in order to understand what I actually pass as variable...
If you need something additionaly please let me know...
Thank you.
Private Sub LoadTreeView(ByVal ds As DataSet)
        Dim oTreeView As TreeView = New TreeView()
        Dim oDataRow As DataRow
        oTreeView = Me.TreeView1

        For Each oDataRow In ds.Tables("dtCosts1").Rows

            If oDataRow("Reports") = -1 Then
                Dim oNode As New System.Windows.Forms.TreeNode
                oNode.Tag() = oDataRow("ID")
                oNode.Text = oDataRow("ID") & (" " + oDataRow("TCID_name").ToString())
                oTreeView.Nodes.Add(oNode)

Call Show_filters (Me.ComboBox1.Text,RTRIM(oNode.Text))

                RecursivelyLoadTree(oDataRow, oNode)
            End If
        Next oDataRow
        Controls.Add(oTreeView)

    End Sub

Open in new window

Commented:
Hello alsam,

I guess I got it now. Replace the FindNode function with the attached one. As I see, you used a RTrim when you pass oNode.Text value to the childnode argument. Maybe the FindNode function is not finding any node because the childnode argument is being different from the oNode.Text. In this case i´ve modified the FindNode function to do a Trim and a ToLower in both oNode.Text and childnode. This will increase match probability. Also, to avoid stopping the application in case the node is not found, add the following code line between "Dim node As TreeNode = FindNode(childnode, oTreeView.Nodes(0))" and "node.Checked = True" in the Show_filters method:

If node Is Nothing Then Continue For

That error occurred because the FindNode didn´t return any value since a node with a specified text was not found, wich lead to a Null Reference Exception. That line of code will skip the "node.Checked = True" line in case the FindNode function does not return any value. Try this. If it does not work, we jump to another solution rather than this one.
Function FindNode(ByVal Text As String, ByVal Node As TreeNode) As TreeNode
        Dim n As TreeNode = Nothing
        If Node.Text.Trim.ToLower() = Text.Trim.ToLower() Then
            n = Node
        Else
            If Node.Nodes.Count > 0 Then
                For Each nd As TreeNode In Node.Nodes
                    If n IsNot Nothing Then Exit For
                    n = FindNode(Text, nd)
                Next
            End If
        End If
        Return n
    End Function

Open in new window

Author

Commented:
Perfect.
It works.
Thank you very much for  your time and effort.
Regards.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial