TreeView and Sorted Lists?

R

rolf.oltmans

Hello,

I need to populate a TreeView control from SortedList(Winforms
application). I've stored ID's as key and Nodes as value. A typical
TreeView in my case would like like

1 Goal
1.1 Task1
1.2Task2
1.2.1 Goal1
1.2.2 sample task
1.2.3 sample txt
1.3 sample txt
2 sample txt
2.1 sample txt
2.2 sample txt
2.3 sample txt
2.3.1 sample txt
2.3.2 sample txt
3 sample txt
4 sample txt
and so on.

Following values are stored as keys
1
1.1
1.2
1.2.1
1.2.2
1.2.3
1.3
2
2.1
2.2
2.3
2.3.1
2.3.2
3
4

So I've tried hard but couldn't come up with a solution that would
generate TreeView from values in SortedList. Please note that tree
should exactly look like the structure of Arraylist, based on ID's.

If you think that I am approaching this problem from the wrong side
then please let me know what is the standard way to populate treeview
control from datastructures like hash table and SortedList. I did
search a lot on this problem but being naive couldn't retreive
something substantial. Please pardon my ignorance and enlighten me on
this issue.
I will really appreciate any help.
 
N

Nicholas Paldino [.NET/C# MVP]

Rolf,

It doesn't really matter if you have a SortedList, or something else
here. Your keys are really paths through the tree (1 is the first node
under the root, 1.1 is the node beneath node 1, etc, etc).

What I would do is cycle through your items in your SortedList, and
parse the key into segments. For example, for 1.2.1 ("Goal1"), I would
parse that into an array of three elements (1, 2, 1).

Then, what you would do is at the root, look for node 1. If it doesn't
exist, create it. Then in node 1 (assuming you found it, or created it),
look for node 2. If it doesn't exist, then create it and add it as a child
to node 1), and so on, and so on.

Once you have the final node (through creating it, or finding it), you
would assign the value to it.

Hope this helps.
 
R

rolf.oltmans

Nicholas,

I appreciate your quick reply. However, I just partially understood
your message/solutions.
I will really appreciate if you can explain a little more. A pseudocode
would do great.

Thanks,
Oltmans

Rolf,

It doesn't really matter if you have a SortedList, or something else
here. Your keys are really paths through the tree (1 is the first node
under the root, 1.1 is the node beneath node 1, etc, etc).

What I would do is cycle through your items in your SortedList, and
parse the key into segments. For example, for 1.2.1 ("Goal1"), I would
parse that into an array of three elements (1, 2, 1).

Then, what you would do is at the root, look for node 1. If it doesn't
exist, create it. Then in node 1 (assuming you found it, or created it),
look for node 2. If it doesn't exist, then create it and add it as a child
to node 1), and so on, and so on.

Once you have the final node (through creating it, or finding it), you
would assign the value to it.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I need to populate a TreeView control from SortedList(Winforms
application). I've stored ID's as key and Nodes as value. A typical
TreeView in my case would like like
1 Goal
1.1 Task1
1.2Task2
1.2.1 Goal1
1.2.2 sample task
1.2.3 sample txt
1.3 sample txt
2 sample txt
2.1 sample txt
2.2 sample txt
2.3 sample txt
2.3.1 sample txt
2.3.2 sample txt
3 sample txt
4 sample txt
and so on.
Following values are stored as keys
1
1.1
1.2
1.2.1
1.2.2
1.2.3
1.3
2
2.1
2.2
2.3
2.3.1
2.3.2
3
4
So I've tried hard but couldn't come up with a solution that would
generate TreeView from values in SortedList. Please note that tree
should exactly look like the structure of Arraylist, based on ID's.
If you think that I am approaching this problem from the wrong side
then please let me know what is the standard way to populate treeview
control from datastructures like hash table and SortedList. I did
search a lot on this problem but being naive couldn't retreive
something substantial. Please pardon my ignorance and enlighten me on
this issue.
I will really appreciate any help.
 
N

Nicholas Paldino [.NET/C# MVP]

Rolf,

1) cycle through sorted list keys
2) parse keys into array of elements (e.g. {1, 2, 1})
3) have a current node variable, set to root node.
4) look for next element in array
a) if it exists, set current node to node
b) if it doesn't exist, create it, and add it to current node. Then set
current node to new node
5) Go to next element in array, then repeat 4)
6) When there are no more elements in the array, set the text of the node to
the value provided by the key.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Nicholas,

I appreciate your quick reply. However, I just partially understood
your message/solutions.
I will really appreciate if you can explain a little more. A pseudocode
would do great.

Thanks,
Oltmans

Rolf,

It doesn't really matter if you have a SortedList, or something else
here. Your keys are really paths through the tree (1 is the first node
under the root, 1.1 is the node beneath node 1, etc, etc).

What I would do is cycle through your items in your SortedList, and
parse the key into segments. For example, for 1.2.1 ("Goal1"), I would
parse that into an array of three elements (1, 2, 1).

Then, what you would do is at the root, look for node 1. If it
doesn't
exist, create it. Then in node 1 (assuming you found it, or created it),
look for node 2. If it doesn't exist, then create it and add it as a
child
to node 1), and so on, and so on.

Once you have the final node (through creating it, or finding it),
you
would assign the value to it.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I need to populate a TreeView control from SortedList(Winforms
application). I've stored ID's as key and Nodes as value. A typical
TreeView in my case would like like
1 Goal
1.1 Task1
1.2Task2
1.2.1 Goal1
1.2.2 sample task
1.2.3 sample txt
1.3 sample txt
2 sample txt
2.1 sample txt
2.2 sample txt
2.3 sample txt
2.3.1 sample txt
2.3.2 sample txt
3 sample txt
4 sample txt
and so on.
Following values are stored as keys
1
1.1
1.2
1.2.1
1.2.2
1.2.3
1.3
2
2.1
2.2
2.3
2.3.1
2.3.2
3
4
So I've tried hard but couldn't come up with a solution that would
generate TreeView from values in SortedList. Please note that tree
should exactly look like the structure of Arraylist, based on ID's.
If you think that I am approaching this problem from the wrong side
then please let me know what is the standard way to populate treeview
control from datastructures like hash table and SortedList. I did
search a lot on this problem but being naive couldn't retreive
something substantial. Please pardon my ignorance and enlighten me on
this issue.
I will really appreciate any help.
 

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