2014-09-12 172 views
0

我有一個DataTable,可以有1到14列,我想。我希望創建一個TreeView,以便可以更好地對所呈現的數據進行排序。問題是,當我通過數據表循環時,它沒有正確格式樹。實際上,它只獲取根節點和子節點,但它多次添加子節點。現在我可以蠻橫地逼它工作,但這是低效的。所以如果有人能幫助我。將子節點添加到VB中的TreeView上的Childe節點

A data table that is used What it should look like in the tree

代碼:(忽略.Count - 5

Public Sub loadTreeView() 

    compClass.treeView.Nodes.Clear() 

    Dim row As DataRow 
    Dim parentNode As TreeNode 
    Dim childNode As TreeNode 

    Dim count As Integer = compClass.dataTable.Columns.Count - 5 

    Dim test(count) As TreeNode 

    For Each row In compClass.dataTable.Rows 

     For i As Integer = 0 To count 

      If i = 0 Then 
       test(i) = searchNode(row.Item(i).ToString()) 
       If test(i) IsNot Nothing Then 
       Else 
        test(i) = New TreeNode(row.Item(i).ToString()) 
        compClass.treeView.Nodes.Add(test(i)) 
       End If 
       'parentNode = searchNode(row.Item(0).ToString()) 
       'If parentNode IsNot Nothing Then 
       'Else 
       ' parentNode = New TreeNode(row.Item(0).ToString()) 
       ' compClass.treeView.Nodes.Add(parentNode) 
       'End If 
      Else 
       childNode = searchNode(row.Item(i - 1).ToString()) 
       If childNode IsNot Nothing Then 
        test(i) = New TreeNode(row.Item(i).ToString()) 
        test(i - 1).Nodes.Add(test(i)) 
       End If 
       'parentNode = searchNode(row.Item(i - 1).ToString()) 
       'If parentNode IsNot Nothing Then 
       ' childNode = New TreeNode(row.Item(i).ToString()) 
       ' parentNode.Nodes.Add(childNode) 
       'End If 
      End If 
     Next 
    Next 
End Sub 

Private Function searchNode(ByVal nodeText As String) 
    For Each node As TreeNode In compClass.treeView.Nodes 
     If node.Text = nodeText Then 
      Return node 
     End If 
    Next 

End Function 
+0

它假設看起來像表佈局下的圖像。 – CodeMonkey 2014-09-15 14:27:45

回答

2

我想,如果你想在你想要的父母第一 例如添加「樹添加子來告訴你9「到」2014「 您將」2014「設置爲父節點,然後添加」9「

並且如果要在樹視圖中搜索節點,請使用Nodes.Find鍵),搜索節點的搜索方法僅搜索根節點。

終於在添加節點時使用密鑰,它知道父節點和當前節點是否重複的方式。

例如: 2014 - > 9 - > AL
「9」還有關鍵的是「20149」 當添加Al元素檢查「20149」是存在的,如果是的父節點,然後檢查鍵「20149AL 「如果不存在則添加節點‘AL’......等

試試這個代碼,它的工作,我:)

Public Sub loadTreeView() 
    compClass.treeView.Nodes.Clear() 
    Dim parentNode As TreeNode 
    Dim childNode As TreeNode 
    Dim count As Integer = compClass.dataTable.Columns.Count - 1 
    Dim row As DataRow 
    Dim ItemKey As String 

    For Each row In compClass.dataTable.Rows 
     Dim test(count) As TreeNode 
     ItemKey = Nothing 
     For i As Integer = 0 To count 
      If i = 0 Then 
       test(i) = searchNode(row.Item(i).ToString()) 
       If test(i) IsNot Nothing Then 
       Else 
        test(i) = New TreeNode(row.Item(i).ToString()) 
        compClass.treeView.Nodes.Add(row.Item(i).ToString(), row.Item(i).ToString()) 'Add item and key for item 
       End If 
       ItemKey = row.Item(i).ToString() 
      Else 
       parentNode = searchNode(ItemKey) 
       childNode = searchNode(ItemKey & row.Item(i).ToString()) 

       If childNode IsNot Nothing Then 
        ItemKey &= row.Item(i).ToString() 
       ElseIf parentNode IsNot Nothing Then 
        test(i) = New TreeNode(row.Item(i).ToString()) 
        parentNode.Nodes.Add(ItemKey & row.Item(i).ToString(), row.Item(i).ToString()) 
        ItemKey &= row.Item(i).ToString() 
       End If 
      End If 
     Next 
    Next 
End Sub 

Private Function searchNode(ByVal nodeKey As String) As TreeNode 
    Dim FoundNodes As TreeNode() = compClass.treeView.Nodes.Find(nodeKey, True) 
    If FoundNodes IsNot Nothing AndAlso FoundNodes.Length > 0 Then 
     Return FoundNodes(0) 
    End If 
    Return Nothing 
End Function 
+0

我可以試試這個。它看起來很穩固。我會盡快通知你。 – CodeMonkey 2014-09-15 14:29:42

+0

這工作完美!非常感謝你! – CodeMonkey 2014-09-15 18:56:11

+0

您的歡迎;) – Jala 2014-09-15 18:59:29

相關問題