469,282 Members | 1,772 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Populate treeview from database

Hello all,

First off all, I appollogize for my englisch.
I have a problem.
I have a table in my sql database with 2 fields.
Field 1 is an ID, and Field 2 contains the complete file locations.

What I would like to do is create a treeview from this table.
For example:
Table:
Id FileLocation
1 D:\Demo\001\01\00001.tiff
2 D:\Demo\001\01\00002.tiff
3 D:\Demo\002\01\00001.tiff
4 D:\Demo\002\01\00002.tiff
5 D:\Demo\002\01\1\00001.tiff
6 D:\Demo\002\02\00001.tiff
7 D:\Demo\002\02\00002.tiff
8 D:\Demo\003\01\00001.tiff
9 D:\Demo\003\01\00002.tiff

The treeview would look something like this:
- D:\
- Demo
- 001
- 01
- 0001.tiff
- 0002.tiff
- 002
- 01
- 0001.tiff
- 0002.tiff
- 1
- 0003.tiff
- 002
- 02
- 0001.tiff
- 0002.tiff
- 003
- 01
- 0001.tiff
- 0002.tiff

As you can see I don't know how deep the tree can be.
What I do know is that they all have the same root level : D:\Demo\

I hope someone can help me with this problem.
I would appreciate any help.

Thank you,
Nathan

Aug 29 '06 #1
2 3446
Hi -

One of three options:

1. If using SQL then write a stored procedure to generate an extra column
being the level of each node. Still need to process the information in order
to update the tree. Doesn't make it that much easier in the client
application as even if nodes are on the same level then they may have
different parentage.

2. Retrieve the data and write logic in the application to process the
records calculating the level of each node using Path.GetDirectoryName for
each nodes FileLocation.

last="D:\"

while(reader.Read())
{
current=Path.GetDirectoryName(reader.FileLocation) .
If current=last then
// node is on the same level. add node to end of current nodes
parent.
If current.length>last.length and last.substring(current.length)=current
then
// node is below the current level. create nodes as appropriate and
add the new node.
Else
// node has different parentage. Need to work out from current
location what nodes need to be added (probably easiest to work from the
root).
End if

last = current
}

As you can see the code above in 2 is iterative and providing you keep track
of the last nodes position you should easily be able to update the tree.

3. Alternatively you could add each node walking the nodes in the tree from
the root adding (or re-using) nodes as appropriate.

HTH

- Andy

<th*******@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Hello all,

First off all, I appollogize for my englisch.
I have a problem.
I have a table in my sql database with 2 fields.
Field 1 is an ID, and Field 2 contains the complete file locations.

What I would like to do is create a treeview from this table.
For example:
Table:
Id FileLocation
1 D:\Demo\001\01\00001.tiff
2 D:\Demo\001\01\00002.tiff
3 D:\Demo\002\01\00001.tiff
4 D:\Demo\002\01\00002.tiff
5 D:\Demo\002\01\1\00001.tiff
6 D:\Demo\002\02\00001.tiff
7 D:\Demo\002\02\00002.tiff
8 D:\Demo\003\01\00001.tiff
9 D:\Demo\003\01\00002.tiff

The treeview would look something like this:
- D:\
- Demo
- 001
- 01
- 0001.tiff
- 0002.tiff
- 002
- 01
- 0001.tiff
- 0002.tiff
- 1
- 0003.tiff
- 002
- 02
- 0001.tiff
- 0002.tiff
- 003
- 01
- 0001.tiff
- 0002.tiff

As you can see I don't know how deep the tree can be.
What I do know is that they all have the same root level : D:\Demo\

I hope someone can help me with this problem.
I would appreciate any help.

Thank you,
Nathan

Aug 29 '06 #2
Hi Andy,

Thank you for your answer.
I think that your option 2 can work for me.
I tried to figure it out how to populate and where to create new
childnodes,
but i can't seem to get it to work.

Would you be so kind to give me an working sample code ?

Thank you.
Nathan
Andy Bates schreef:
Hi -

One of three options:

1. If using SQL then write a stored procedure to generate an extra column
being the level of each node. Still need to process the information in order
to update the tree. Doesn't make it that much easier in the client
application as even if nodes are on the same level then they may have
different parentage.

2. Retrieve the data and write logic in the application to process the
records calculating the level of each node using Path.GetDirectoryName for
each nodes FileLocation.

last="D:\"

while(reader.Read())
{
current=Path.GetDirectoryName(reader.FileLocation) .
If current=last then
// node is on the same level. add node to end of current nodes
parent.
If current.length>last.length and last.substring(current.length)=current
then
// node is below the current level. create nodes as appropriate and
add the new node.
Else
// node has different parentage. Need to work out from current
location what nodes need to be added (probably easiest to work from the
root).
End if

last = current
}

As you can see the code above in 2 is iterative and providing you keep track
of the last nodes position you should easily be able to update the tree.

3. Alternatively you could add each node walking the nodes in the tree from
the root adding (or re-using) nodes as appropriate.

HTH

- Andy

<th*******@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Hello all,

First off all, I appollogize for my englisch.
I have a problem.
I have a table in my sql database with 2 fields.
Field 1 is an ID, and Field 2 contains the complete file locations.

What I would like to do is create a treeview from this table.
For example:
Table:
Id FileLocation
1 D:\Demo\001\01\00001.tiff
2 D:\Demo\001\01\00002.tiff
3 D:\Demo\002\01\00001.tiff
4 D:\Demo\002\01\00002.tiff
5 D:\Demo\002\01\1\00001.tiff
6 D:\Demo\002\02\00001.tiff
7 D:\Demo\002\02\00002.tiff
8 D:\Demo\003\01\00001.tiff
9 D:\Demo\003\01\00002.tiff

The treeview would look something like this:
- D:\
- Demo
- 001
- 01
- 0001.tiff
- 0002.tiff
- 002
- 01
- 0001.tiff
- 0002.tiff
- 1
- 0003.tiff
- 002
- 02
- 0001.tiff
- 0002.tiff
- 003
- 01
- 0001.tiff
- 0002.tiff

As you can see I don't know how deep the tree can be.
What I do know is that they all have the same root level : D:\Demo\

I hope someone can help me with this problem.
I would appreciate any help.

Thank you,
Nathan
Sep 1 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Jacob John | last post: by
reply views Thread by T.H.M | last post: by
4 posts views Thread by Mike | last post: by
3 posts views Thread by Jan Wrage | last post: by
9 posts views Thread by Marina | last post: by
6 posts views Thread by Beginner | last post: by
reply views Thread by Patrick | last post: by
reply views Thread by xmail123 | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.