Ciao a tutti.
Io ho una tabella di nome <helper> con questi campi:
0 key (long) che rappresenta la name del nodo
1 titolo (text 255) che rappresenta la text del nodo
2 key_rif (long) che rappresenta la name del nodo del quale questo nodo è child (per esempio se il valore è 7 vuol dire che questo nodo è child del nodo denominato 7)
Devo popolare una TreeView dalla tabella e ho scritto questo codice:
Private Function bCaricaTreeView1(ByVal sStrSQL As String) As DataTable
Dim dt As New DataTable
Dim sConnTV As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source = c:\helper\ark.accdb"
Using con As New OleDbConnection(sConnTV)
Using cmd As New OleDbCommand(sStrSQL)
Using sda As New OleDbDataAdapter()
cmd.CommandType = CommandType.Text
cmd.Connection = con
sda.SelectCommand = cmd
sda.Fill(dt)
End Using
End Using
Return dt
End Using
End Function
Private Sub ptreeview(sDtParent As DataTable, sParentID As Integer, tn1 As TreeNode)
For Each row As DataRow In sDtParent.Rows
Dim tnChild As New TreeNode() With {
.Text = row("titolo").ToString(),
.Name = row("key")
}
If sParentID = 0 Then
TreeView1.Nodes.Add(tnChild)
Dim dtChild As DataTable = Me.bCaricaTreeView1("SELECT key, titolo FROM helper WHERE key_rif = " & tnChild.Name)
ptreeview(dtchild, Convert.ToInt32(tnChild.Name), tnChild)
Else
tn1.Nodes.Add(tnChild)
End If
If Val(tnChild.Name) > lKey Then
lKey = Val(tnChild.Name)
End If
Next
End Sub
Quando devo popolare scrivo:
Dim dt As DataTable = Me.bCaricaTreeView1("SELECT key, titolo FROM helper WHERE key_rif = 0")
Me.ptreeview(dt, 0, Nothing)
Funziona tutto molto bene fino a che i livelli dei nodi sono 2 (principale e child) se devo creare un child di child non va più bene e non lo inserisce nella TreeView.
Dove sbaglio?