Private Sub LoadTreeView()
Try
trvTreeView.BeginUpdate()
trvTreeView.Nodes.Clear()
Sort out the root nodes
Dim ViewByPC As New TreeNode("View by PC", 0, 0)
ViewByPC.Tag = "View"
Dim ViewByApp As New TreeNode("View by Application", 0, 0)
ViewByApp.Tag = "View"
Dim nodes() As TreeNode = {ViewByPC, ViewByApp}
Start doing the View by PC Name nodes stuff
For Each row As DataRow In tblTargetPCs.Rows
Create new node holding the names of the PCs from the target PCs table
Dim PCNode As New TreeNode(Convert.ToString(row.Item(0)), 1, 1)
PCNode.Text = PCNode.Text.ToUpper
Set the nodes tag to PC, this is so that it can be easily identified when clicked.
PCNode.Tag = "PC"
For Each PCRow As DataRow In tblDomainPCs.Rows
If Convert.ToString(PCRow(0)).ToUpper = PCNode.Text Then
If Convert.ToBoolean(PCRow.Item(2)) Then
PCNode.ImageIndex = 4
PCNode.SelectedImageIndex = 4
End If
End If
Next
ViewByPC.Nodes.Add(PCNode)
Next
Dim NodeCount, t As Integer
NodeCount = ViewByPC.GetNodeCount(False) - 1
Dim NodesToRemove(NodeCount) As Integer
II = NodeCount
I = 0
t = 0
Do Until I >= NodeCount
Do Until II <= 0
Check for any duplicate PC name nodes and, if any, remove them
If II = I Then II -= 1
If II <= 0 Then Exit Do
If ViewByPC.Nodes(II).Text = ViewByPC.Nodes.Item(I).Text Then
ViewByPC.Nodes(II).Remove()
NodeCount -= 1
End If
II -= 1
Loop
I += 1
II = NodeCount
If I >= NodeCount Then Exit Do
Loop
For I As Integer = 0 To tblTargetPCs.Rows.Count - 1
For II As Integer = 0 To ViewByPC.GetNodeCount(False) - 1
Add the applications installed on a PC into that PCs node
If Convert.ToString(tblTargetPCs.Rows(I).Item(0)) = ViewByPC.Nodes.Item(II).Text Then
Dim AppNode As New TreeNode(Convert.ToString(tblTargetPCs.Rows.Item(I).Item(1)), 2, 2)
AppNode.Tag = "App"
ViewByPC.Nodes(II).Nodes.Add(AppNode)
End If
Next
Next
Now start doing the View by Application stuff.
Add all (if any) of the applications from the Target PCs applications row into nodes to be parented by the View by Application node.
For I As Integer = 0 To tblTargetPCs.Rows.Count - 1
Dim AppNodes As New TreeNode(Convert.ToString(tblTargetPCs.Rows.Item(I).Item(1)), 2, 2)
AppNodes.Tag = "App"
ViewByApp.Nodes.Add(AppNodes)
Next
NodeCount = ViewByApp.GetNodeCount(False) - 1
I = 0
II = NodeCount
Remove any duplicate nodes.
Do Until I >= NodeCount
Do Until II <= 0
If II = I Then II -= 1
If II <= 0 Then Exit Do
If ViewByApp.Nodes(II).Text = ViewByApp.Nodes(I).Text Then
ViewByApp.Nodes(I).Remove()
NodeCount -= 1
End If
II -= 1
Loop
I += 1
II = NodeCount
If I >= NodeCount Then Exit Do
Loop
Now, add the PCs that the applications are installed on, underneath the Application nodes.
For I As Integer = 0 To tblTargetPCs.Rows.Count - 1
For II As Integer = 0 To ViewByApp.GetNodeCount(False) - 1
If Convert.ToString(tblTargetPCs.Rows(I).Item(1)) = ViewByApp.Nodes(II).Text Then
Dim PCNode As New TreeNode(Convert.ToString(tblTargetPCs.Rows(I).Item(0)), 1, 1)
PCNode.Text = PCNode.Text.ToUpper
PCNode.Tag = "PC"
For Each PCrow As DataRow In tblDomainPCs.Rows
If Convert.ToString(PCrow(0)).ToUpper = PCNode.Text Then
If Convert.ToBoolean(PCrow.Item(2)) Then
PCNode.ImageIndex = 4
PCNode.SelectedImageIndex = 4
End If
End If
Next
ViewByApp.Nodes(II).Nodes.Add(PCNode)
End If
Next
Next
Add the nodes into the treeview
trvTreeView.Nodes.AddRange(nodes)
Catch ex As Exception
MessageBox.Show(ex.ToString)
Finally
trvTreeView.EndUpdate()
End Try
End Sub