473,726 Members | 2,177 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Does anybody have a real-world sample of building a treeview control

Does anybody have a real-world sample of buiding a treeview control using
data from database tables? All the sample code I have found either builds
the treeview manually or uses a file directory as the sample.

I have trouble translating those samples to working with data coming from my
database, so I am hoping that someone could show me a sample of code that
builds a tree from say a dataset.

Many controls you can bind to a datasource, but treeviews, I think, would be
more difficult because of the node structure. How would the application
tell what piece of data belonged at what node level? That is why I would
imagine that you would have to add siginificant code to build the control
up. Am I correct in that assumption?

Here is looking forward to some samples from the gurus out there. Thanks.
Jun 5 '06 #1
4 2312
It would depend on what kind of data you want to represent, but at home
I have a program I wrote in C# that stores all my novels in a database,
and represents them in a treeview. Each node is added individually to
the tree. This is a simplistic view of the data schema:

Book : This is the actual novel
Series : All books belong to a series
Group : All Series belong to a group (such as "Science Fiction")
Groups can belong to other groups. For example, the "TSR Novels" group
belongs to the "Science Fiction" group which belongs to the "Fiction"
group.

At run time, the tree displays a root node and under that is all groups
that don't have a parent group id. Then each of those groups adds any
child groups. This is done through "recursion" . In all cases, the
program is just iterating through the datatable of groups looking for
any group that has the parent ID of the group it is filling nodes for.
Its slow but, I don't have that many books!

Sorry I don't have code here for you. But its not uncommon to create
MENUs with this kind of concept. Try making a Menu table like this in
your DB:

MenuID int
MenuName nvarchar 50
ParentMenuID int

Usually I would store -1 in ParentMenuID for menu's that are the top
level. Other's might prefer to store NULL instead, or 0, or some other
predetermined negative number.

Add in some fake values manually. Then in your code, return ALL the
rows from your menu table into a datatable. Create your first "ROOT"
node for your treeview. Then put it as a parameter to your recursive
function that may look something like this:

private DataTable AllMenus;

private void LoadNodes(TreeN ode ParentNode, int ParentMenuID) {
foreach (DataRow dr in AllMenus.Rows) {
if (dr["ParentMenu ID"] == ParentMenuID) {
TreeNode newNode = new TreeNode(dr["MenuName"]);
TreeNode.Tag = dr;
ParentNode.Node s.Add(newNode);
LoadNodes(newNo de, dr["MenuID"]);
}
}
}

When you call "LoadNodes" the first time with your root node, just pass
-1 as the ParentMenuID.

(DISCLAIMER: Code not tested, and note that my type conversions are not
completed.

Hope this helps you out!

Steve
Henry wrote:
Does anybody have a real-world sample of buiding a treeview control using
data from database tables? All the sample code I have found either builds
the treeview manually or uses a file directory as the sample.

I have trouble translating those samples to working with data coming from my
database, so I am hoping that someone could show me a sample of code that
builds a tree from say a dataset.

Many controls you can bind to a datasource, but treeviews, I think, would be
more difficult because of the node structure. How would the application
tell what piece of data belonged at what node level? That is why I would
imagine that you would have to add siginificant code to build the control
up. Am I correct in that assumption?

Here is looking forward to some samples from the gurus out there. Thanks.


Jun 5 '06 #2
Henry wrote:
Does anybody have a real-world sample of buiding a treeview control using
data from database tables? All the sample code I have found either builds
the treeview manually or uses a file directory as the sample.

I have trouble translating those samples to working with data coming from my
database, so I am hoping that someone could show me a sample of code that
builds a tree from say a dataset.

Many controls you can bind to a datasource, but treeviews, I think, would be
more difficult because of the node structure. How would the application
tell what piece of data belonged at what node level? That is why I would
imagine that you would have to add siginificant code to build the control
up. Am I correct in that assumption?

Here is looking forward to some samples from the gurus out there. Thanks.


We have experimented with these two in our last project:

http://www.codeproject.com/cs/miscctrl/dbTree.asp
http://www.codeproject.com/cs/miscct...chicaltree.asp

HTH,
Andy
Jun 5 '06 #3
My table has a parent_id data element. The root node has a value of -1 all
other nodes have the value of the id data element as the parent_id. The id
column / data lement represents the key field for the table. They are unique
ids. Then there is the name column / data element which is the text I want
to be visible in the tree..

I am not sure how to recurse through the dataset. Can I set filters ? Which
ADO object am I manipulating? The table? the datarow?

When a user chooses a node I need to pass the id to other controls
"Steven Nagy" <le*********@ho tmail.com> wrote in message
news:11******** **************@ u72g2000cwu.goo glegroups.com.. .
It would depend on what kind of data you want to represent, but at home
I have a program I wrote in C# that stores all my novels in a database,
and represents them in a treeview. Each node is added individually to
the tree. This is a simplistic view of the data schema:

Book : This is the actual novel
Series : All books belong to a series
Group : All Series belong to a group (such as "Science Fiction")
Groups can belong to other groups. For example, the "TSR Novels" group
belongs to the "Science Fiction" group which belongs to the "Fiction"
group.

At run time, the tree displays a root node and under that is all groups
that don't have a parent group id. Then each of those groups adds any
child groups. This is done through "recursion" . In all cases, the
program is just iterating through the datatable of groups looking for
any group that has the parent ID of the group it is filling nodes for.
Its slow but, I don't have that many books!

Sorry I don't have code here for you. But its not uncommon to create
MENUs with this kind of concept. Try making a Menu table like this in
your DB:

MenuID int
MenuName nvarchar 50
ParentMenuID int

Usually I would store -1 in ParentMenuID for menu's that are the top
level. Other's might prefer to store NULL instead, or 0, or some other
predetermined negative number.

Add in some fake values manually. Then in your code, return ALL the
rows from your menu table into a datatable. Create your first "ROOT"
node for your treeview. Then put it as a parameter to your recursive
function that may look something like this:

private DataTable AllMenus;

private void LoadNodes(TreeN ode ParentNode, int ParentMenuID) {
foreach (DataRow dr in AllMenus.Rows) {
if (dr["ParentMenu ID"] == ParentMenuID) {
TreeNode newNode = new TreeNode(dr["MenuName"]);
TreeNode.Tag = dr;
ParentNode.Node s.Add(newNode);
LoadNodes(newNo de, dr["MenuID"]);
}
}
}

When you call "LoadNodes" the first time with your root node, just pass
-1 as the ParentMenuID.

(DISCLAIMER: Code not tested, and note that my type conversions are not
completed.

Hope this helps you out!

Steve
Henry wrote:
Does anybody have a real-world sample of buiding a treeview control using
data from database tables? All the sample code I have found either
builds
the treeview manually or uses a file directory as the sample.

I have trouble translating those samples to working with data coming from
my
database, so I am hoping that someone could show me a sample of code that
builds a tree from say a dataset.

Many controls you can bind to a datasource, but treeviews, I think, would
be
more difficult because of the node structure. How would the application
tell what piece of data belonged at what node level? That is why I would
imagine that you would have to add siginificant code to build the control
up. Am I correct in that assumption?

Here is looking forward to some samples from the gurus out there.
Thanks.

Jun 6 '06 #4
I am slightly confused by what you are saying.
How about you post your table schema, indicating which is the primary
key, which is the parent key (foreign key) and which is the text you
want to display?
Try and replace those values with the ones I have provided as an
example.

Henry wrote:
My table has a parent_id data element. The root node has a value of -1 all
other nodes have the value of the id data element as the parent_id. The id
column / data lement represents the key field for the table. They are unique
ids. Then there is the name column / data element which is the text I want
to be visible in the tree..

I am not sure how to recurse through the dataset. Can I set filters ? Which
ADO object am I manipulating? The table? the datarow?

When a user chooses a node I need to pass the id to other controls
"Steven Nagy" <le*********@ho tmail.com> wrote in message
news:11******** **************@ u72g2000cwu.goo glegroups.com.. .
It would depend on what kind of data you want to represent, but at home
I have a program I wrote in C# that stores all my novels in a database,
and represents them in a treeview. Each node is added individually to
the tree. This is a simplistic view of the data schema:

Book : This is the actual novel
Series : All books belong to a series
Group : All Series belong to a group (such as "Science Fiction")
Groups can belong to other groups. For example, the "TSR Novels" group
belongs to the "Science Fiction" group which belongs to the "Fiction"
group.

At run time, the tree displays a root node and under that is all groups
that don't have a parent group id. Then each of those groups adds any
child groups. This is done through "recursion" . In all cases, the
program is just iterating through the datatable of groups looking for
any group that has the parent ID of the group it is filling nodes for.
Its slow but, I don't have that many books!

Sorry I don't have code here for you. But its not uncommon to create
MENUs with this kind of concept. Try making a Menu table like this in
your DB:

MenuID int
MenuName nvarchar 50
ParentMenuID int

Usually I would store -1 in ParentMenuID for menu's that are the top
level. Other's might prefer to store NULL instead, or 0, or some other
predetermined negative number.

Add in some fake values manually. Then in your code, return ALL the
rows from your menu table into a datatable. Create your first "ROOT"
node for your treeview. Then put it as a parameter to your recursive
function that may look something like this:

private DataTable AllMenus;

private void LoadNodes(TreeN ode ParentNode, int ParentMenuID) {
foreach (DataRow dr in AllMenus.Rows) {
if (dr["ParentMenu ID"] == ParentMenuID) {
TreeNode newNode = new TreeNode(dr["MenuName"]);
TreeNode.Tag = dr;
ParentNode.Node s.Add(newNode);
LoadNodes(newNo de, dr["MenuID"]);
}
}
}

When you call "LoadNodes" the first time with your root node, just pass
-1 as the ParentMenuID.

(DISCLAIMER: Code not tested, and note that my type conversions are not
completed.

Hope this helps you out!

Steve
Henry wrote:
Does anybody have a real-world sample of buiding a treeview control using
data from database tables? All the sample code I have found either
builds
the treeview manually or uses a file directory as the sample.

I have trouble translating those samples to working with data coming from
my
database, so I am hoping that someone could show me a sample of code that
builds a tree from say a dataset.

Many controls you can bind to a datasource, but treeviews, I think, would
be
more difficult because of the node structure. How would the application
tell what piece of data belonged at what node level? That is why I would
imagine that you would have to add siginificant code to build the control
up. Am I correct in that assumption?

Here is looking forward to some samples from the gurus out there.
Thanks.


Jun 6 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1355
by: GregD | last post by:
Our recent director of IT for the City I work for is trying to get some traction in the arena of collaborative programming to replace applications like real estate tax assessment, treasurer tax collection, codes and compliance permit issuance, voter registrar vote tally web reporting for the masses, etc. Originally, he had hoped the emphasis wouldbe on open source products like PHP, Pyhthon, and the like, but we are now finding that many...
2
1100
by: Richard Trahan | last post by:
I've had a couple of my posts disappear recently, so I can't respond to responders without registering with Developersdex, the price of which is eternal advertisements. Maybe I'll try Google groups. Is anybody else having this problem?
58
30235
by: Larry David | last post by:
Ok, first of all, let's get the obvious stuff out of the way. I'm an idiot. So please indulge me for a moment. Consider it an act of "community service".... What does "64bit" mean to your friendly neighborhood C# programmer? The standard answer I get from computer sales people is: "It means that the CPU can process 64 bits of data at a time instead of 32." Ok... I guess I *kind* of understand what that means at an intuitive level, but what...
5
1335
by: Geoff Jones | last post by:
Hi Does anybody use the Visual Basic Resource Kit? I have a question about it and I've either discovered a bug in it or have a problem with my installation. Geoff
12
1765
by: Terry Olsen | last post by:
VB.NET doesn't seem to go over very well with the recreational users out in inet land. I've got a few "free" programs that I put out for people to use, and I get emails like "it'd be a nice utility if it wasn't written in a child's language", or "no way I'm installing the .NET Framework on my PC', "Why don't you use a REAL compiler like GCC, LCC, or Pelles C?". Seems like the attitude is that "REAL" programmers only write programs where...
130
6588
by: Daniel Manes | last post by:
I'm baffled. I have a column in a SQL Server Express database called "Longitude," which is a float. When I view the table in a DataGridView, some of the numbers, which only have two decimal places in the database show up with *15* decimal places and are ever so slightly off (in the example in the subject line, by about 2E-15). I'm not doing any operations on this column. It's just running a stored procedure which performs a pretty basic...
0
1213
by: =?Utf-8?B?U3Vwcml5YQ==?= | last post by:
Oracle has a migration utility that lets you convert an application in Access to Oracle forms. See <http://www.oracle.com/technology/products/database/application_express/ viewlets/apex_quicktour_viewlet.html> Does Microsoft have a similar utility, one that can convert Access forms and reports to .Net ? I am looking at migrating an application built in Access to a .Net web application real fast without having to go thru' the whole...
3
1824
by: Debbie Croft | last post by:
Does C# have the ability to define array properties, as in Delphi? For example, does the following have an equivalent in C#? type TMyGrid = class { ...some code... } property IntegerCells: Integer read GetIntegerCell write SetIntegerCell;
65
3602
by: Aditya | last post by:
Hi I am using a line of as char recvedValues = {'\0'}; in my code. I get a warning as near initialization for recvedValues. I am using gcc 3.4 Can anybody please explain the meaning. Thanks Aditya
1
2747
by: donbock | last post by:
I develop embedded software for avionics. It is not unheard of for the software we develop to be used for 20+ years and to be ported to several different platforms over the years (a real example: Amiga PC -> 68000 -> 68020 -> 68040 -> PowerPC -> another PowerPC). As a result, we strive to write portable code. ISO/IEC TR 18037:2008 specifies a series of extensions of the programming language C, which is specified by ISO/IEC 9899:1999. These...
0
8890
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
8752
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
9402
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...
0
9259
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9120
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6702
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4521
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...
1
3228
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
3
2157
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.