Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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 didn't work)

 

How would I do that for any nodes?

That's as clear as I can make my question, I hope you understand it.

 

Thanks in advance

  • *Experts*
Posted

You'll 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 - I'm going off of VB6 here :)). If it doesn't 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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

Thanks for the help, but I have only just figured it out, I had it partially going the way I was previously doing it. Here's how I did it:

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!

  • 3 months later...
Posted

This is the entire chunk of code that I used to do it:

 

   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 node's 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 PC's 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 haven't bothered rewriting it yet, but it does the trick nicely.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...