当前位置:网站首页>VB: treelist control of dev control - get all nodes of treelist (recursive)

VB: treelist control of dev control - get all nodes of treelist (recursive)

2020-11-10 14:46:13 Mo Xiaoxuan

This article will introduce the acquisition of TreeList all Node Node method , here TreeList Using data source binding , For dynamic access to , And for TreeList Set up comboBox Check box , Dynamically select from the database TreeList The node of

One 、 First of all, let's start with TreeList Bind data source , And set up KeyFieldName as well as ParentFieldName

TreeList1.DataSource = DataTable1
TreeList1.KeyFieldName = " Number "
TreeList1.ParentFieldName = " Superior number "

Two 、 Query the conditions or data that need to check the check box from the database , I won't go into more details here , Just call BLL Query the database and put the returned results in a DataTable in . It's called dt Detailed

3、 ... and 、 First, go through the first layer TreeList

For i As Integer = 0 To TreeList1.Rows.Count - 1
      For j As Integer = 0 To dt Detailed .Rows.Count - 1
            If TreeList1.Nodes(i).Item(" Number ").ToString = dt Detailed .Rows.Item(j).Item(" Number ").ToString Then
                  ' Set the node to selected 
                  TreeList1.Nodes.Item(i).CheckState = CheckState.Checked
                  Exit For
            End If
      Next j
      ' Call recursive functions 
      getAllNodes(TreeList1.Nodes(i))
Next i

Four 、 Next, start writing recursive methods getAllNodes()

Public Sub getAllNodes(ParentNode As TreeListNode)
      Dim i As Integer = 0
      Try
            'node Represents the current node , It is used to query whether there are subordinate nodes 
            For Each node As TreeListNode In ParentNode.Nodes
                  For j As Integer = 0 To dt Detailed .Rows.Count - 1
                        If TreeList1.Nodes(i).Item(" Number ").ToString = dt Detailed .Rows.Item(j).Item(" Number ").ToString Then
                              ' Set the node to selected 
                              TreeList1.Nodes.Item(i).CheckState = CheckState.Checked
                              Exit For
                        End If
                  Next j
                  ' Determine whether there is a subordinate node 
                  If node.Nodes.Count > 0 Then
                        ' Query the next level node 
                        getAllNodes(node)
                  End If
                  i = i + 1
             Next
      Catch ex As Exception
            MsgBox(ex.ToString)
      End Try
End Sub

The above completed the requirements of this article , I am still programming Xiaobai , If there is a mistake and a better way , I hope you can correct me more , Thank you for reading

版权声明
本文为[Mo Xiaoxuan]所创,转载请带上原文链接,感谢