recursive function

A

Andy

Hi all,

i am pretty new to programming and have a (simple?) problem here:
I want to populate a Treeview with data from a table.
Table layout is:
ID, ParentID, Description

There can be infinite levels, the starting level for ParentID is 0
(this is root level).
I know that I will have to make a recursive function to fill this
treeview, but I have tried and didn't succeed. Can some1 help me
please?

Thanks in advance.

Using VS 2005 and .NET 2.0
 
M

m.posseth

maybe you could start by showing the code that failed . recursive methods
itself are pretty simpel just call the method from within the method untill
a certain condition is met

to give you an idea


private sub increment ( x as integer )
x + =1
if x < 1000 then increment x
end sub

regards

Michel Posseth [MCP]
 
A

Andy

well, I started with something like this, but then I completely got
lost

Private Sub TreeAdd(ByRef n As TreeNode, ByVal ID As Integer)
Dim ds As New DataSet
Dim ds2 As New DataSet
Dim dr As DataRow
Dim drChild As DataRow
Dim ParentNode As TreeNode
Dim ChildNode As TreeNode
Dim rootadapter As System.Data.OleDb.OleDbDataAdapter
Dim rootadapter2 As System.Data.OleDb.OleDbDataAdapter
If n Is Nothing Then ' Root
TreeView1.Nodes.Clear()
rootAdapter = New
System.Data.OleDb.OleDbDataAdapter("SELECT * FROM tbl_Category WHERE
Parent_Category_ID__ = 0", conn)
rootAdapter.Fill(ds, "Main")
For Each dr In ds.Tables(0).Rows
ParentNode = New TreeNode(dr.Item("Category").ToString)
ParentNode.Tag = dr.Item("category_ID__").ToString
TreeView1.Nodes.Add(ParentNode)
' Child dieser einhängen
rootadapter2 = New
System.Data.OleDb.OleDbDataAdapter("SELECT * FROM tbl_Category WHERE
Parent_Category_ID__ =" & dr.Item("Category_id__"), conn)
rootAdapter2.Fill(ds2, "Main")
For Each drChild In ds2.Tables(0).Rows
ChildNode = New
TreeNode(drChild.Item("Category").ToString)
ChildNode.Tag =
drChild.Item("category_ID__").ToString
ParentNode.Nodes.Add(ChildNode)
TreeAdd(ChildNode, drChild.Item("category_id__"))
Next

Next
End If
End Sub

any help is appreciated

thanks
Andy
 
B

Bob

Take a look at
http://www.windowsitpro.com/Articles/Index.cfm?ArticleID=8826&DisplayTab=Article

I found it helpful in understanding how to manage the data in a hierarchy
like you describe.
If you modify your table structure and triggers as explained in that article
then one simple sql statement gives you the Hierarchy view of the table
contents.

In my case I used
SELECT tblEmployeeId, mgrid, lvl, Hierarchy, REPLICATE(' ', lvl) +
EmployeeCode AS Employee, EmployeeName
FROM tblEmployees
and just loaded that in a combobox. You can see the hierarchy because each
child is offset from it's parent by two spaces.

Of course this assumes you're working with sql server, there are no triggers
to do that maintenance of hierarchies in Access AFIK

HTH
Bob
 
C

Cyril Gupta

Hello Andy,

I had exactly the same problem some days ago: fill a treeview from Database.

This is the code I wrote.

For Each dr In dRows 'Iterate through the collection of Rows
nd = New TreeNode
nd.Text = Convert.ToString(dr.Item("accoMast_AccName")) 'Store Account
Name
nd.Tag = Convert.ToString(dr.Item("accoMast_AccNum")) 'Store the account
number

If dr.Item("accoMast_LevelNum") = 0 Then 'If the Level is 0 then there
is no parent node
tvwAccounts.Nodes.Add(nd) 'Add the node
hRows.Add(dr.Item("accoMast_AccNum"), nd.Index) 'Keep information
about the node in the hashtable
Else
Dim iIndex As Integer 'Create a variable to hold the index number
iIndex = hRows.Item(dr.Item("accomast_GroupNum")) 'Retrieve the
group account number for the account
tvwAccounts.Nodes(iIndex).Nodes.Add(nd) 'Add the account to the
nodes of the treeview
hRows.Add(dr.Item("accoMast_AccNum"), nd.Index) 'Add the details to
the hashtable
End If
Next

Here's the logic...

I have retrieved a DataRow collection filled with my data and I iterate
through each of the DataRow object.

In my databse I have some important fields

1. LevelNum
2. IndexNum
3. GroupNum

LevelNum - The position of the item in the treeview hierarchy, Root is 0,
and then add 1 for each level
Indexnum - The position of the item in its peers
GroupNum - The Parent Node of this item

If you read the code carefully you will find that with this information I
can fill up a treeview with infinite records, to infinite hierarchy with
just that single loop.

Hope it helps

Cyril
 
A

Andy

Hi Bob,

not exactly what I need, but an interesting workaround :)
Im afraid the solution must work for both Access and SQL Server

thanks

Andy
 
A

Andy

Hi Cyril,

thanks for your answer. Maintaining an additional field just for the
level information is not a very good solution. You get more overhead
when moving nodes and a bigger database as well. (me coming from the db
side is a bit sensitive about wasting disk space LOL)
nontheless, I got a working version of such a function somewhere
around. It's from my Delphi life and I just need to translate it (the
hard part for me as .NET beginner)

greetings

Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top