none
Remplissage de Treeview RRS feed

  • Question

  • Bonjour, 

    J'ai un treeview que j'ai rempli à partir de ma base de données, mais la méthode que j'ai utilisé est nulle, vous allez tout de suite remarqué que c'est fais par un débutant, donc ce que je cherche c'est une méthode plus efficace, pour remplir treeview à partir de la base de données et aussi enregistrer les données de ce dernier dans la base de données.

    Voila mon code :


     
    Imports MySql.Data.MySqlClient
     
     
    Public Class FrmUser
        Dim MyCmd As MySqlCommand
        Dim MyDr As MySqlDataReader
        Dim Requette As String
        Dim z1, z2, z3, z4, z5, z6, z7, z8, z9 As String
        Dim doneByDoubleClick As Boolean = False
     
        Private Sub FrmUser_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Try
                Requette = "select distinct nom from usertable where noeud<>'x' order by nom"
                MyCmd = New MySqlCommand(Requette, MyDb)
                MyDr = MyCmd.ExecuteReader
                While MyDr.Read()
                    List1.Items.Add(MyDr.GetValue(0).ToString())
                End While
                MyDr.Close()
     
                RemplirTreeView()
     
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
     
        Private Sub List1_Click(sender As Object, e As EventArgs) Handles List1.SelectedIndexChanged
            TxtUser.Text = List1.SelectedItem
            TxtUser.Enabled = False
            RemplirTreeView()
        End Sub
        Private Sub RemplirTreeView()
            Try
                TreeView1.Nodes.Clear()
                If TxtUser.Text = "" And TxtPwd.Text = "" Then Exit Sub
                TxtPwd.Text = ""
                Requette = "select distinct pass from usertable where  nom='" & TxtUser.Text & "' and app='" & My.Application.Info.AssemblyName & "' and autoris<>3"
                MyCmd = New MySqlCommand(Requette, MyDb)
                MyDr = MyCmd.ExecuteReader
                While MyDr.Read()
                    TxtPwd.Text = MyDr.GetValue(0).ToString()
                End While
                MyDr.Close()
     
                Requette = "select * from usertable where app='" & My.Application.Info.AssemblyName & "' and nom='" & TxtUser.Text & "' order by cle"
                MyCmd = New MySqlCommand(Requette, MyDb)
                MyDr = MyCmd.ExecuteReader
                Dim hasRows As Integer = MyDr.HasRows
                    While MyDr.Read()
                        Dim wCle As String = MyDr.GetValue(4).ToString()
                        Dim wCaption As String = MyDr.GetValue(6).ToString()
                        Dim wAutoris As Integer = MyDr.GetValue(5) - 1
                        z1 = wCle.Substring(0, 2)
                        With TreeView1
                            If wCle.Length = 2 Then
                                .Nodes.Add(wCle, wCaption, wAutoris, wAutoris)
                            ElseIf wCle.Length = 3 Then
                                .Nodes(z1).Nodes.Add(wCle, wCaption, wAutoris, wAutoris)
                            ElseIf wCle.Length = 4 Then
                                z2 = wCle.Substring(0, 3)
                                .Nodes(z1).Nodes(z2).Nodes.Add(wCle, wCaption, wAutoris, wAutoris)
                            ElseIf wCle.Length = 5 Then
                                z3 = wCle.Substring(0, 4)
                                .Nodes(z1).Nodes(z2).Nodes(z3).Nodes.Add(wCle, wCaption, wAutoris, wAutoris)
                            ElseIf wCle.Length = 6 Then
                                z4 = wCle.Substring(0, 5)
                                .Nodes(z1).Nodes(z2).Nodes(z3).Nodes(z4).Nodes.Add(wCle, wCaption, wAutoris, wAutoris)
                            ElseIf wCle.Length = 7 Then
                                z5 = wCle.Substring(0, 6)
                                .Nodes(z1).Nodes(z2).Nodes(z3).Nodes(z4).Nodes(z5).Nodes.Add(wCle, wCaption, wAutoris, wAutoris)
                            ElseIf wCle.Length = 8 Then
                                z6 = wCle.Substring(0, 7)
                                .Nodes(z1).Nodes(z2).Nodes(z3).Nodes(z4).Nodes(z5).Nodes(z6).Nodes.Add(wCle, wCaption, wAutoris, wAutoris)
                            ElseIf wCle.Length = 9 Then
                                z7 = wCle.Substring(0, 8)
                                .Nodes(z1).Nodes(z2).Nodes(z3).Nodes(z4).Nodes(z5).Nodes(z6).Nodes(z7).Nodes.Add(wCle, wCaption, wAutoris, wAutoris)
                            ElseIf wCle.Length = 10 Then
                                z8 = wCle.Substring(0, 9)
                                .Nodes(z1).Nodes(z2).Nodes(z3).Nodes(z4).Nodes(z5).Nodes(z6).Nodes(z7).Nodes(z8).Nodes.Add(wCle, wCaption, wAutoris, wAutoris)
                            ElseIf wCle.Length = 10 Then
                                z9 = wCle.Substring(0, 10)
                                .Nodes(z1).Nodes(z2).Nodes(z3).Nodes(z4).Nodes(z5).Nodes(z6).Nodes(z7).Nodes(z8).Nodes(z9).Nodes.Add(wCle, wCaption, wAutoris, wAutoris)
                            End If
                        End With
                    End While
     
                MyDr.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
     
        Private Sub TreeView1_NodeMouseDoubleClick(sender As Object, e As TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick
            doneByDoubleClick = True
            Dim i As Integer = TreeView1.SelectedNode.SelectedImageIndex
            i = i + 1
            If i > 2 Then i = 0
            TreeView1.SelectedNode.SelectedImageIndex = i
            TreeView1.SelectedNode.ImageIndex = i
            Dim myNode As TreeNode = e.Node
            GetChildNodes(myNode, i)
     
        End Sub
     
        Sub GetChildNodes(tnode As TreeNode, i As Integer)
     
            For Each node As TreeNode In tnode.Nodes
                node.ImageIndex = i
                If tnode.Nodes.Count > 0 Then GetChildNodes(node, i)
            Next
     
        End Sub
     
        Private Sub TreeView1_BeforeCollapse(sender As Object, e As TreeViewCancelEventArgs) Handles TreeView1.BeforeCollapse, TreeView1.BeforeExpand
            If doneByDoubleClick Then
                e.Cancel = True
                doneByDoubleClick = False
            End If
        End Sub
     
        Private Sub TreeView1_BeforeExpand(sender As Object, e As TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand
            If doneByDoubleClick Then
                e.Cancel = True
                doneByDoubleClick = False
            End If
        End Sub
        Private Sub BtnEnregistrer_Click(sender As Object, e As EventArgs) Handles BtnEnregistrer.Click
     
            If TxtUser.Text = "" Then Exit Sub
     
            Requette = "delete from usertable where app='" & My.Application.Info.AssemblyName & "' and nom='" & TxtUser.Text & "'"
            MyCmd = New MySqlCommand(Requette, MyDb)
            MyDr = MyCmd.ExecuteReader
            MyDr.Close()
     
            For Each node1 As TreeNode In TreeView1.Nodes
                'requete pour enregister les données du parents'
                For Each node2 As TreeNode In node1.Nodes
                    'requete pour enregister les données du 1er enfants '
                    For Each node3 As TreeNode In node2.Nodes
                         'requete pour enregister les données du 2eme enfants'
                        For Each node4 As TreeNode In node3.Nodes
                            ' ...'
                            For Each node5 As TreeNode In node4.Nodes
                                ' ...'
                                For Each node6 As TreeNode In node5.Nodes
                                    ' ...'
                                    For Each node7 As TreeNode In node6.Nodes
                                        ' ...'
                                        For Each node8 As TreeNode In node7.Nodes
                                            ' ...'
                                            For Each node9 As TreeNode In node8.Nodes
                                                ' ...'
                                            Next
                                        Next
                                    Next
                                Next
                            Next
                        Next
                    Next
                Next
            Next
     
        End Sub


    Merci d'avance

    mercredi 6 juin 2018 14:45