Dataset to treeview

I_R_Lee

Well-known member
Joined
Jul 18, 2002
Messages
50
Location
Sydney, Australia
In a project I am working on at the moment (in VB .NET), it is using an untyped dataset which is reading an xml file, this all works perfectly. I know how to get this information to the treeview, but how do I remove duplicate nodes and move their children into a node with the same name, so that this:

-View by PC Name
_____-MAT_NB2K00
__________-Symantec PCAnywhere
_____-MAT_NB2K00
__________-Microsoft Visual Studio .NET Enterprise Architect

Would become this:

-View by PC Name
_____-MAT_NB2K00
__________-Symantec PCAnywhere
__________-Microsoft Visual Studio .NET Enterprise Architect

(The underscore things are for indentation, spaces didnt work)

How would I do that for any nodes?
Thats as clear as I can make my question, I hope you understand it.

Thanks in advance
 
Youll need to code an XPath query that eliminates dupes, mostly likely through a transformation. The alternative is to use some code-based check to see if a node has already been added. This could be through a key (if the .NET tree supports it - Im going off of VB6 here :)). If it doesnt support a Key, you can either make your own class that inherits from TreeNode or embed something in the Tag of each node.

By the way, you can wrap your code in [ code ] blocks (minus the spaces) to format it better.

-nerseus
 
Thanks for the help, but I have only just figured it out, I had it partially going the way I was previously doing it. Heres how I did it:
Code:
Dim ViewByPC As New TreeNode("View by PC Name", 0, 0)
Dim ViewByApp As New TreeNode("View by Application", 0, 0)

NodeCount = ViewByPC.GetNodeCount(True)
            II = 0
            I = 0
            Do Until I > NodeCount - 1
                Do Until II > NodeCount - 1
                     Check for any duplicate PC name nodes and, if any, remove them
                    If II = I Then II += 1
                    If II > NodeCount - 1 Then Exit Do
                    If ViewByPC.Nodes(II).Text = ViewByPC.Nodes(I).Text Then
                        ViewByPC.Nodes(II).Remove()
                        NodeCount -= 1
                    End If
                    II += 1
                Loop
                I += 1
                II = 0
            Loop
Yay!
 
Thanks for sharing this code but what I cant get it to do is move the children to the remaining parents.

How did you manage to do that??

Thanks in advance
gs
 
This is the entire chunk of code that I used to do it:

Code:
    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

It might be a bit messy, I havent bothered rewriting it yet, but it does the trick nicely.
 
Back
Top