473,480 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

recursive function

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
8 3636
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2866
by: | last post by:
OK: Purpose: Using user's input and 3 recursive functions, construct an hour glass figure. Main can only have user input, loops and function calls. Recursive function 1 takes input and displays...
4
2408
by: Nicolas Vigier | last post by:
Hello, I have in my python script a function that look like this : def my_function(arg1, arg2, opt1=0, opt2=1, opt3=42): if type(arg1) is ListType: for a in arg1: my_function(a, arg2,...
4
9034
by: Victor | last post by:
Hello, I've got a situation in which the number of (valid) recursive calls I make will cause stack overflow. I can use getrlimit (and setrlimit) to test (and set) my current stack size. ...
9
13141
by: Bill Borg | last post by:
Hello, I call a function recursively to find an item that exists *anywhere* down the chain. Let's say I find it five layers deep. Now I've got what I need and want to break out of that whole...
9
16790
by: Csaba Gabor | last post by:
Inside a function, I'd like to know the call stack. By this I mean that I'd like to know the function that called this one, that one's caller and so on. So I thought to do: <script...
41
3309
by: Harry | last post by:
Hi all, 1)I need your help to solve a problem. I have a function whose prototype is int reclen(char *) This function has to find the length of the string passed to it.But the conditions...
10
2535
by: AsheeG87 | last post by:
Hello Everyone! I have a linked list and am trying to include a recursive search. However, I am having trouble understanding how I would go about that. I don't quite understand a recursive...
4
2101
by: ThEoNeAnDOnLy | last post by:
I recently had an issue with my recursive project in class. Here is the code. // Recursion.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include...
3
4210
by: from.future.import | last post by:
Hi, I encountered garbage collection behaviour that I didn't expect when using a recursive function inside another function: the definition of the inner function seems to contain a circular...
3
2322
by: Davy | last post by:
Hi all, Sometimes I need to pass same parameter in recursive function. From my point of view, the style is redundant, and I don't what to use some global style like self.A, self.B, Is there any...
0
7046
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7088
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6741
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4783
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4485
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2986
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
183
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.