如何在Treeview控件中按名称分组

时间:2017-04-15 10:05:42

标签: vb.net visual-studio

我一直在谷歌搜索并搜索youtube但没有找到答案,所以我想在这里寻求帮助。

我想在VB中的TreeView控件中对名称进行分组,我该怎么办?

谢谢

Visual Studio Form GUI Visual Studio Form GUI 2

Public Class FrmPengingat

    Private Sub FrmPengingat_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DSLap.PengingatHutang' table. You can move, or remove it, as needed.
        Me.PengingatHutangTableAdapter.Fill(Me.DSLap.PengingatHutang)

        'fill the tree control
        Dim NmPemasok As String
        Dim NoNota As String
        Dim TglJatuhTempo As Date
        Dim Total As Decimal
        Dim tmpNmPemasok As String

        Dim i As Integer
        For i = 0 To DSLap.PengingatHutang.Count - 1
            NmPemasok = DSLap.PengingatHutang.Rows(i).Item(0)
            tmpNmPemasok = DSLap.PengingatHutang.Rows(i).Item(0)
            NoNota = DSLap.PengingatHutang.Rows(i).Item(1)
            TglJatuhTempo = DSLap.PengingatHutang.Rows(i).Item(2)
            Total = DSLap.PengingatHutang.Rows(i).Item(3)

            TreeView1.Nodes.Add(i, NmPemasok).Nodes.Add(i, NoNota & " (" & TglJatuhTempo & ")   " & Total)

        Next
    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

这一行:

TreeView1.Nodes.Add(i, NmPemasok).Nodes.Add(i, NoNota & " (" & TglJatuhTempo & ")   " & Total)

每次都添加一个新的父节点。您应该检查树视图是否已包含具有相同文本的节点,如果不是,则仅添加新节点。

我建议您使密钥与父节点的文本相同,这样就可以更容易地检查它是否存在。

将上述内容替换为:

If TreeView1.Nodes.ContainsKey(NmPemasok) Then 'The parent node already exists.
    TreeView1.Nodes(NmPemasok).Nodes.Add(i, NoNota & " (" & TglJatuhTempo & ")   " & Total)
Else 'The parent node doesn't exist.
    TreeView1.Nodes.Add(NmPemasok, NmPemasok).Nodes.Add(i, NoNota & " (" & TglJatuhTempo & ")   " & Total)
End If