473,383 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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 2266
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(TreeNode ParentNode, int ParentMenuID) {
foreach (DataRow dr in AllMenus.Rows) {
if (dr["ParentMenuID"] == ParentMenuID) {
TreeNode newNode = new TreeNode(dr["MenuName"]);
TreeNode.Tag = dr;
ParentNode.Nodes.Add(newNode);
LoadNodes(newNode, 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*********@hotmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.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(TreeNode ParentNode, int ParentMenuID) {
foreach (DataRow dr in AllMenus.Rows) {
if (dr["ParentMenuID"] == ParentMenuID) {
TreeNode newNode = new TreeNode(dr["MenuName"]);
TreeNode.Tag = dr;
ParentNode.Nodes.Add(newNode);
LoadNodes(newNode, 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*********@hotmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.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(TreeNode ParentNode, int ParentMenuID) {
foreach (DataRow dr in AllMenus.Rows) {
if (dr["ParentMenuID"] == ParentMenuID) {
TreeNode newNode = new TreeNode(dr["MenuName"]);
TreeNode.Tag = dr;
ParentNode.Nodes.Add(newNode);
LoadNodes(newNode, 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
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...
2
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...
58
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...
5
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
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...
130
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...
0
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/...
3
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:...
65
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...
1
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:...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.