By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,239 Members | 1,503 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,239 IT Pros & Developers. It's quick & easy.

recursive function

P: n/a
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

Jan 11 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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]

"Andy" <us****@andreas-schubert.net> schreef in bericht
news:11*********************@g49g2000cwa.googlegro ups.com...
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

Jan 11 '06 #2

P: n/a
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

Jan 11 '06 #3

P: n/a
Bob
Take a look at
http://www.windowsitpro.com/Articles...layTab=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
"Andy" <us****@andreas-schubert.net> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
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

Jan 11 '06 #4

P: n/a
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
Jan 12 '06 #5

P: n/a
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

Jan 12 '06 #6

P: n/a
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

Jan 12 '06 #7

P: n/a
Andy,

Is this what you mean (it has nothing to do with recursive however creates a
treeview from a datatable)

http://www.vb-tips.com/default.aspx?...f-04dfd3b31b2b

I hope this helps,

Cor
Jan 12 '06 #8

P: n/a
You might want to check out
http://msdn.microsoft.com/library/de...cntrlsamp3.asp.

Another option you could consider playing with would be a self-referential
DataRelation within a DataSet. I haven't tested it personally yet, but
conceptually it should work. The trick is indicating the root node.

Jim

"Andy" <us****@andreas-schubert.net> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
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

Jan 13 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.