Populate treeview from database

T

thanneman

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
 
A

Andy Bates

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
 
T

thanneman

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:
 

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