Bonjour à tous,

J'ai un soucis pour remplir un Treeview à partir d'une base SQL.

Quand je mets mes données dans une structure, je récupère bien mes 27 lignes de ma base mais quand je construit mes treeview je ne récupère que 9 enregistrement, en fait je me fait ejecter lorsque j'arrive dans les enfants du 2e noeud.

Voici mon code pour le load de la fenetre :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
 Private Sub uc_test_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim Articles(30) As s_article    'Permet de travailler sur un tableau de 100 adresses
        Dim I_Article As Int32 = 0
 
 
 
 
 
        ds = New DataSet
        Dim dt As DataTable = DevFactDS.REFERENTIEL
 
        REFERENTIELTableAdapter.Fill(Me.DevFactDS.REFERENTIEL)
 
        Dim Root_Level As TreeNode = Nothing
        Dim Level_One As TreeNode = Nothing
        Dim Level_Two As TreeNode = Nothing
        Dim Level_Three As TreeNode = Nothing
        Dim ParentNode As TreeNode = Nothing
        Dim Child_Level As TreeNode = Nothing
 
        Dim sql As String = "SELECT * FROM REFERENTIEL ORDER BY PARENT"
        Dim dr As System.Data.Common.DbDataReader = Nothing
 
        Dim twRoot As String = String.Empty
        Dim twChild As String = String.Empty
        Dim parent As String = String.Empty
        Dim node_text As String = String.Empty
 
        Try
 
            dr = DBUtils.dbInfo.DBUtils.ExecuteReader(CommandType.Text, sql)
 
            While dr.Read
                Articles(I_Article).ID = dbConversion.ConvertToInt32(dr.Item(0))
                Articles(I_Article).Libelle = dr.Item(1).ToString
                Articles(I_Article).Parent = dbConversion.ConvertToInt32(dr.Item(2))
 
                I_Article += 1
 
                parent = dr.Item(2).ToString
                If parent = "0" Then
                    twRoot = dr.Item(1).ToString
                    Root_Level = New TreeNode
                    Root_Level = TreeViewEx1.Nodes.Add(twRoot)
                    Root_Level.Tag = dr.Item(0).ToString
                Else
                    ' Vérification existance niveau 1
                    Dim idParent As String = dr.Item(2).ToString
 
                    ParentNode = FindParent(idParent, TreeViewEx1.Nodes)
                    twChild = dr.Item(1).ToString
                    Level_One = ParentNode.Nodes.Add(twChild)
                End If
            End While
        Catch ex As Exception 'logs.logs.WriteToLog(ex.ToString)
            Return
        Finally
            If Not dr Is Nothing Then dr.Close()
        End Try
    End Sub
et ensuite celui de ma fonction pour rechercher le parent :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
' Function pour rechercher le parent dans le treeview
    Function FindParent(ByVal strSearch As String, ByVal Nodes As TreeNodeCollection) As TreeNode
        Dim ret As TreeNode
 
        '~~> Loop through each TreeNode
        For Each TrNode As TreeNode In Nodes
            Dim nod_tag As String = TrNode.Tag.ToString
 
            '~~> Compare node text with search text
            'If TrNode.Text = strSearch Then
            If nod_tag = strSearch Then
                Return TrNode
            End If
 
            '~~> Do recursive search if there are child nodes
            If TrNode.Nodes.Count > 0 Then
                ret = FindParent(strSearch, TrNode.Nodes)
                If Not ret Is Nothing Then
                    Return ret
                End If
            End If
        Next
        Return Nothing
    End Function
Et voici un apercu de ma base
ID // LIBELLE // ID_PARENT
1 // MENUS - BUFFETS // 0
2 // PRODUIT // 0
3 // DIVERS // 0
4 // MENU 1 PLAT // 1
5 // MENU 2 PLATS // 1
6 // MENU CEREMONIE - PRESTIGE // 1
7 // ENTREE // 2