Tieni presente che non ho mai utilizzato il TreeView.
Comunque, nella pagina metto:
<asp:TreeView ID="TreeView1" runat="server" ShowLines="true" ></asp:TreeView>
che popolo da codice:
codice:
Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
'questo codice va nell'evento load della pagina
Dim dt As DataTable = libreria.GetDataTable(StringaConnessione, "select * from albero ")
Dim nodo As New TreeNode("Pietro", "0")
nodo.SelectAction = TreeNodeSelectAction.Expand
Me.TreeView1.Nodes.Add(nodo)
albero2(0, dt, nodo)
End Sub
Private Sub albero2(ByVal id As Integer, ByVal dt As DataTable, ByVal nodo As TreeNode)
Dim rows As DataRow() = dt.Select(String.Format("padre = {0}", id))
For i As Integer = 0 To rows.Length - 1
Dim childNode As New TreeNode(rows(i).Item("nome").ToString(), CStr(rows(i).Item("id")))
childNode.SelectAction = TreeNodeSelectAction.Expand
nodo.ChildNodes.Add(childNode)
albero2(CInt(rows(i).Item("id")), dt, childNode)
Next
End Sub