vb.net中,我需要用n个级别更快地从dbase表填充treeview

f1tvaqid  于 2021-08-13  发布在  Java
关注(0)|答案(0)|浏览(182)

我需要从odbc数据库得到的数据表中编译一个treeview。该表包含compid、parentcompid和compname,所有字符串。我的table大约有4500行。dbase表示例
我在互联网上找到了一些代码,并对其进行了修改,使其适合我,我能够填充树视图,但由于树视图中的子女和孙子孙女,我需要大约7分钟(在我的程序中不可行的)。我已经坚持了一个星期了,找不到更快的方法了。我是vb.net初学者。有人能帮我吗?或者给我指个方向?谢谢。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    TreeView1.Visible = False

    Dim PrSet As DataSet = PDataset("SELECT  CompID, ParentCompID, CompName, CompNo FROM [Component] Where CHAR_LENGTH(CompNo) = 14 AND (ParentCompID IS NOT NULL)")
    TreeView1.BeginUpdate()
    TreeView1.Nodes.Clear()
    DataGridView5.DataSource = PrSet.Tables(0)
    For Each dr As DataRow In PrSet.Tables(0).Rows
        If (dr("ParentCompID")) = 1040965 Then
            Dim tnParent As New TreeNode()
            tnParent.Text = dr("CompNo").ToString().Trim() & " " & dr("CompName").ToString().Trim()
            Dim value As String = dr("CompID").ToString()
            tnParent.Expand()
            TreeView1.Nodes.Add(tnParent)
            FillChild(tnParent, value)

        End If
    Next

    TreeView1.Sort()
    TreeView1.Visible = True
    TreeView1.EndUpdate()

End Sub
Public Function FillChild(parent As TreeNode, IID As String) As Integer

    Dim ds As DataSet = PDataset("SELECT CompID, ParentCompID, CompName, CompNo FROM Component WHERE ParentCompID =" + IID)
    If ds.Tables(0).Rows.Count > 0 Then
        For Each dr As DataRow In ds.Tables(0).Rows
            Dim child As New TreeNode()
            child.Text = dr("CompNo").ToString().Trim() & " " & dr("CompName").ToString().Trim()
            Dim temp As String = dr("CompID").ToString()
            child.Collapse()
            parent.Nodes.Add(child)
            FillChild(child, temp)
        Next
        Return 0
    Else
        Return 0
    End If

End Function
Protected Function PDataset(select_statement As String) As DataSet
    Dim _con As New OdbcConnection("Dsn=amos")
    Dim ad As New OdbcDataAdapter(select_statement, _con)
    Dim ds As New DataSet()
    ad.Fill(ds)
    _con.Close()
    Return ds
End Function

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题