Populate TreeView from DATABASE table -
07-10-2008
, 11:42 AM
Hello, I would like some help in order to populate treeview from a database
table the structure of the table is as fallows ID (string), PARENTID(String),
DESCRIPTION (string),
rows sample
'1','0','activo'
'11','1','hello'
'31','3','hello'
VB.net 2008
I've been working arround a code I found on the web, but cant get it to work.
at first run
' Create Parent's NODES on Load
Private Sub FormPlanCashFlow_Load(ByVal sender As Object, ByVal e As System.
EventArgs) Handles Me.Load
Call FormLogin.Carga_Coneccion()
TreeView1.Nodes.Clear()
TreeView1.Nodes.Add(New TreeNode("Catalogo_Contable"))
Dim tNode As New TreeNode
tNode = TreeView1.Nodes(0)
PopulateTreeView(0, tNode)
tNode.Expand()
Call FormLogin.Close_Coneccion()
End Sub
' Recursive function to fill treeview
Private Sub PopulateTreeView(ByVal inParentID As String, ByRef inTreeNode
As TreeNode)
Call FormLogin.Carga_Coneccion()
dsAccountSQL1.Clear()
daAccount1.SelectCommand = New SqlCommand("SELECT id, description,
parent_acct_id FROM accounts WHERE parent_acct_id= '" & inParentID & "'",
FormLogin.ConnSqlServer)
daAccount1.Fill(dsAccountSQL1, "CatContable")
Dim count As Integer
count = dsAccountSQL1.Tables("CatContable").Rows.Count
'drLoadData1.Table.Clear()
Dim ParentTable As DataTable = New DataTable("CatContable")
ParentTable = dsAccountSQL1.Tables("CatContable")
If count > 0 Then
For Each ModuleParameters.drLoadData1 In ParentTable.Rows
Dim parentnode As TreeNode
Dim strLabel As String = ModuleParameters.drLoadData1.Item(0)
& "--" & ModuleParameters.drLoadData1.Item(1)
parentnode = New TreeNode(strLabel)
inTreeNode.Nodes.Add(parentnode)
parentnode.Tag = ModuleParameters.drLoadData1(0)
PopulateTreeView(ModuleParameters.drLoadData1.Item (0),
parentnode)
Next
ElseIf count = 0 Then
PopulateTreeView("0", TreeView1.Nodes(0))
End If
Call FormLogin.Close_Coneccion()
End Sub |