469,649 Members | 1,400 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,649 developers. It's quick & easy.

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 2102
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Richard Trahan | last post: by
12 posts views Thread by Terry Olsen | last post: by
reply views Thread by =?Utf-8?B?U3Vwcml5YQ==?= | last post: by
3 posts views Thread by Debbie Croft | last post: by
65 posts views Thread by Aditya | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.