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 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
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
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
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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,...
|
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. ...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 ...
|
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...
| |