在VB.NET中从SQL填充TreeView

时间:2019-06-12 07:50:56

标签: sql vb.net treeview

我正在从SQL Server数据库中读取一个字符串列,其中包含长度为3的倍数的可变长度字符串,例如“ 000”,“ 000001”,“ 000002001”,...

我想将数据库的此字段转换为TreeView,其中每个3位代码应引用我们TreeView中的一个级别。例如:

000
→ 000001
→ 000002
001
→ 001001
002
→ 002001
→ 002002
→→ 002002001
→→ 002002002
003
004
→ 004001001001
 and so on...

我已经使用选择案例结构来首先找到3位数字字段并填充TreeView的第一级,然后找到以“ 000”开头的6位数字字符串并填充TreeView ans的第二级,依此类推。

Ds_Edit.Reset()
        con = New SqlConnection(strcon)
        con.Open()
        StrSql = "SELECT Unit_PCode, Unit_Code, Unit_Desc, IsEndNode, Unit_Image FROM cmms_Unit Where IsEndNode=0 Order BY Unit_Code"
        Da = New SqlDataAdapter(StrSql, con)
        Da.Fill(Ds_Edit, "tajhiz")
        con.Close()
        Dim y As String = "0000000000000000000000000000000000000"
        Dim g As String = "0000000000000000000000000000000000000"
        Dim g1 As String = "0000000000000000000000000000000000000"
        Dim g2 As String = "0000000000000000000000000000000000000"
        Dim temp20 As Integer
        temp20 = Ds_Edit.Tables(0).Rows.Count
        Dim str As String
        Dim tedadH As Integer
        Dim node As TreeNode

        For k = 0 To temp20 - 1
            str = Ds_Edit.Tables("tajhiz").Rows(k).Item("Unit_Code")
            tedadH = str.Count

            Select Case tedadH
                Case 3
                    If str = "000" Then
                        TreeView1.Nodes.Add(Ds_Edit.Tables("tajhiz").Rows(k).Item("Unit_Desc"))
                    End If
                Case 6
                    If str Like "000###" Then
                        TreeView1.Nodes(0).Nodes.Add(Ds_Edit.Tables("tajhiz").Rows(k).Item("Unit_Desc"))

                        y = str
                    End If
                Case 9

                    If str Like y & "###" Then
                        Try
                            Dim h1 As Integer = CInt(Mid(str, (tedadH - 5), 3))
                            TreeView1.Nodes(0).Nodes(h1).Nodes.Add(Ds_Edit.Tables("tajhiz").Rows(k).Item("Unit_Desc"))
                            g = str
                        Catch
                        End Try
                    End If

                Case 12
                    If str Like g & "###" Then
                        Try
                            Dim h1 As Integer = CInt(Mid(str, (tedadH - 5), 3))
                            Dim h2 As Integer = CInt(Mid(str, (tedadH - 8), 3))
                            node = TreeView1.Nodes(0).Nodes(h2).Nodes(h1).Nodes.Add(Ds_Edit.Tables("tajhiz").Rows(k).Item("Unit_Desc"))
                            g1 = str
                        Catch
                        End Try
                    End If
                Case 15
                    If str Like g1 & "###" Then
                        Try
                            Dim h1 As Integer = CInt(Mid(str, (tedadH - 5), 3))
                            Dim h2 As Integer = CInt(Mid(str, (tedadH - 8), 3))
                            Dim h3 As Integer = CInt(Mid(str, (tedadH - 11), 3))
                            TreeView1.Nodes(0).Nodes(h3).Nodes(h2).Nodes(h1).Nodes.Add(Ds_Edit.Tables("tajhiz").Rows(k).Item("Unit_Desc"))
                            g2 = str
                        Catch
                        End Try
                    End If
                Case 18
                    If str Like g2 & "###" Then
                        Try
                            Dim h1 As Integer = CInt(Mid(str, (tedadH - 5), 3))
                            Dim h2 As Integer = CInt(Mid(str, (tedadH - 8), 3))
                            Dim h3 As Integer = CInt(Mid(str, (tedadH - 11), 3))
                            Dim h4 As Integer = CInt(Mid(str, (tedadH - 14), 3))
                            TreeView1.Nodes(0).Nodes(h4).Nodes(h3).Nodes(h2).Nodes(h1).Nodes.Add(Ds_Edit.Tables("tajhiz").Rows(k).Item("Unit_Desc"))

                        Catch
                        End Try
                    End If
            End Select
        Next

代码运行没有问题,但是过程需要时间(至少10秒)。我填充Treeview的方法正确吗?

0 个答案:

没有答案