Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So, I was able to combine 2 methods to come up with a way to remove duplicate tree nodes. Right now my problem is I can only do 1 iteration per function I have. I want to be able to use the same function and pick multiple cells to fill the treeview. What I have noew works great if all you want it 1 column generate the treeview with no duplicates. Here is what I have now:

Private Function Searchnode(ByVal nodetext As String, ByVal treeview1 As TreeView) As TreeNode

       Dim newNode As TreeNode = New TreeNode(nodetext)

       For Each node As TreeNode In treeview1.Nodes(0).Nodes
           If node.Text = nodetext Then
               node.Remove()

           End If
       Next

       treeview1.Nodes(0).Nodes.Add(newNode)
       Return newNode


   End Function


   Public Sub Remove_treeview_duplicates()
       TreeView1.Nodes.Add("treenode")

       Dim node As TreeNode

       For Each row As DataRow In Database1DataSet.Table.Rows
           'search in the treeview if node is already present
           node = Searchnode(row.Item(3).ToString(), TreeView1)

       Next

   End Sub


I want to generate the treeview with no duplicate information with certain Row.items, instead of just the 1.

What I have tried:

I made other functions to do the same effects, but there has to be a way for 1 function to control the lot.
Posted
Updated 9-Jan-17 8:43am

1 solution

Not sure if I understand the question correctly but from what I gather you want to remove matching nodes regardless on what level they are found. In order to do that you need a recursion.

Have a look at the following example. First I add some nodes to the tree view and then remove all elements containing text B
VB
    Dim treenode As TreeNode

    ' Add example data
    treenode = Me.TreeView1.Nodes.Add("A")
    treenode.Nodes.Add("B")
    treenode.Nodes.Add("C")

    treenode = Me.TreeView1.Nodes.Add("B")
    treenode = treenode.Nodes.Add("C")
    treenode.Nodes.Add("D")

    treenode = Me.TreeView1.Nodes.Add("C")
    treenode = treenode.Nodes.Add("B")
    treenode.Nodes.Add("A")

    'recursively remove desired nodes
    Me.RemoveNodesByText("B", Me.TreeView1)


End Sub

Private Function RemoveNodesByText(ByVal nodetext As String, ByVal treeview1 As TreeView, Optional childnode As TreeNode = Nothing) As Boolean
    Dim nodesToIterate As TreeNodeCollection

    If childnode Is Nothing Then
        nodesToIterate = treeview1.Nodes
    Else
        nodesToIterate = childnode.Nodes
    End If

    For nodecounter As Integer = nodesToIterate.Count - 1 To 0 Step -1
        ' recursively loop through children
        RemoveNodesByText(nodetext, treeview1, nodesToIterate(nodecounter))
        ' remove this node if matches the given node text
        If nodesToIterate(nodecounter).Text = nodetext Then
            nodesToIterate(nodecounter).Remove()
        End If
    Next

    Return True
End Function

One thing is that in the example code the new node isn't added inside the recursive function. Otherwise multiple new nodes would be created so if you want to ensure that only one node of a kind exists, first remove duplicates and after that add the new node to desired location.
 
Share this answer
 
Comments
Member 11856456 9-Jan-17 14:29pm    
This is a very interesting code, but when I run it based upon the text, it removes all text that is "B". What I am looking to do based on your example, is if the database has 3 B's, I only want 1 B.
Wendelius 9-Jan-17 14:34pm    
Note sure if I understand. Do you add a node for all rows that came from database and you want that only the last remains?

For example
- A fetched from database, A added to nodes
- B fetched from database, B added to nodes
- C fetched from database, C added to nodes
- B fetched from database, preivous B deleted, new B added to nodes
- and so on...
Member 11856456 9-Jan-17 14:44pm    
So once I get the database into the treeview, I have a county column and state column. Example, I used Kentucky as the state and in here I have 3 different counties.

Kentucky-
Floyd
Floyd
Floyd
Floyd
Jefferson
Jefferson
Jefferson
Pike
Pike

what I want to do is remove the duplicated text and keep just 1 of each

Kentucky-
Floyd
Jefferson
Pike

thats just an example, I have other columns I would like to do the same way at the same time.
Wendelius 9-Jan-17 14:53pm    
Okay, the recursive function would basically handle that if you would have the following logic

for each row in datatable
RemoveNodesByText(row("county"), treeview1)
TreeView1.Nodes.Add(row("county"))
next

that would ensure that only the last remains.

However, this won't solve the situation for multiple columns. If you remove nodes also based on text in other columns, what if the combination of columns is unique, should it remain?

The more information you provide the more this sounds like you would need to modify the database query to return only unique combinations. For example if you now have

select county, city, state from mytable

you could use distinct keyword to remove duplicates when fetching the data:

select distinct county, city, state from mytable

This way you wouldn't need to remove already added nodes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900