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

H

Henry

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.
 
S

Steven Nagy

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
 
A

Andreas Mueller

Henry said:
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/miscctrl/bindablehierarchicaltree.asp

HTH,
Andy
 
H

Henry

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 said:
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

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.
 
S

Steven Nagy

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.
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 said:
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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top