将数据库中的重复树节点迭代并删除到树视图中 [英] Iterate and remove duplicate tree nodes from database into treeview

查看:74
本文介绍了将数据库中的重复树节点迭代并删除到树视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我能够结合使用两种方法来找到删除重复树节点的方法。现在我的问题是我每个函数只能进行1次迭代。我希望能够使用相同的功能并选择多个单元格来填充树视图。如果你想要的话,我所拥有的作品很棒1列生成没有重复的树视图。这就是我现在拥有的:



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





我想生成树视图没有与某些Row.items重复的信息,而不仅仅是1.



我尝试过:



我做了其他函数来做同样的效果,但是必须有一种方法来控制该批次。



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.

推荐答案

不确定我是否正确理解了这个问题,但是从我收集的内容中你想要删除匹配的节点,无论它们在什么级别上找到。为了做到这一点,你需要一个递归。



看看下面的例子。首先,我将一些节点添加到树视图中,然后删除包含文本的所有元素 B

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
    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.


这篇关于将数据库中的重复树节点迭代并删除到树视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆