473,802 Members | 2,461 Online
Bytes | Software Development & Data Engineering Community
+ 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 3661
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****@andre as-schubert.net> schreef in bericht
news:11******** *************@g 49g2000cwa.goog legroups.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.Ole Db.OleDbDataAda pter
Dim rootadapter2 As System.Data.Ole Db.OleDbDataAda pter
If n Is Nothing Then ' Root
TreeView1.Nodes .Clear()
rootAdapter = New
System.Data.Ole Db.OleDbDataAda pter("SELECT * FROM tbl_Category WHERE
Parent_Category _ID__ = 0", conn)
rootAdapter.Fil l(ds, "Main")
For Each dr In ds.Tables(0).Ro ws
ParentNode = New TreeNode(dr.Ite m("Category").T oString)
ParentNode.Tag = dr.Item("catego ry_ID__").ToStr ing
TreeView1.Nodes .Add(ParentNode )
' Child dieser einhängen
rootadapter2 = New
System.Data.Ole Db.OleDbDataAda pter("SELECT * FROM tbl_Category WHERE
Parent_Category _ID__ =" & dr.Item("Catego ry_id__"), conn)
rootAdapter2.Fi ll(ds2, "Main")
For Each drChild In ds2.Tables(0).R ows
ChildNode = New
TreeNode(drChil d.Item("Categor y").ToString )
ChildNode.Tag =
drChild.Item("c ategory_ID__"). ToString
ParentNode.Node s.Add(ChildNode )
TreeAdd(ChildNo de, drChild.Item("c ategory_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****@andre as-schubert.net> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.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.ToStrin g(dr.Item("acco Mast_AccName")) 'Store Account
Name
nd.Tag = Convert.ToStrin g(dr.Item("acco Mast_AccNum")) 'Store the account
number

If dr.Item("accoMa st_LevelNum") = 0 Then 'If the Level is 0 then there
is no parent node
tvwAccounts.Nod es.Add(nd) 'Add the node
hRows.Add(dr.It em("accoMast_Ac cNum"), 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.I tem("accomast_G roupNum")) 'Retrieve the
group account number for the account
tvwAccounts.Nod es(iIndex).Node s.Add(nd) 'Add the account to the
nodes of the treeview
hRows.Add(dr.It em("accoMast_Ac cNum"), 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****@andre as-schubert.net> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.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
2894
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 a sequence of spaces; recursive function 2 uses input to display ascending sequence of digits; likewise, recursive function 3 uses input to display descending sequence of digits. I have not followed the instructions completely regarding the...
4
2433
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, opt1=opt1, opt2=opt2, opt3=opt3) return if type(arg2) is ListType:
4
9056
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. However, it is not as straightforward to determine the base address for my stack space. The approach I have taken is to save the address of an automatic variable in main( ), and assume this is a fairly good indicator of my base address. Then, I can...
9
13221
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 stack and continue execution at the point of the initial call. Is that possible? Thanks, Bill
9
16848
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 type='text/javascript'> function myFunc(lev) { // if (lev) return myFunc(lev-1); var aStack=; nextFunc = arguments.callee;
41
3390
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 are that no local variable or global variable should be used.I have to use recursive functions.
10
2573
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 search....would any of you be so kind to explain it to me...maybe include some examples. I would GREATLY appreciate it!!!
4
2128
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 <conio.h> #include <iostream> using namespace std;
3
4246
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 reference, which means it is only collected by the mark-and-sweep collector, not by reference counting. Here is some code that demonstrates it: === def outer():
3
2347
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 other choice? For example, def func(self, x, y, A, B, C): #x, y change in recursive call
0
9699
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9562
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10536
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10285
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
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 we have to send another system
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2966
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.