Filling a treeview

M

MauriZZZZ

Hi folks,

Hope you can help me out.
I want to fill a treeview in VB.Net with values from a table out a
database.
The table has the following contents:

Node - Nodename
000 - Europe
000001 - Belgium
000002 - Spain
000003 - Netherlands
000003001 - Province1
000003002 - Province 2
etc.
001 - Africa
001001 - Egypt
etc.

What is the best way to fill this treeview? I am using a datareader to
loop through the records.

Thanks already!

MauriZZZ
 
L

Lloyd Dupont

What is the best way to fill this treeview? I am using a datareader to
loop through the records.

one node at a time?

in fact I don't see what's your problem, so it's hard to solve...
if you look for a tutorial on TreeNode, look at the SDK documentation, there
is a nice sample.
The table has the following contents:

Node - Nodename
000 - Europe
000001 - Belgium
000002 - Spain
000003 - Netherlands
000003001 - Province1
000003002 - Province 2
etc.
001 - Africa
001001 - Egypt
etc.
Ahum.....
And what is the meaning of these values?
 
M

Morten Wennevik

Hi MauriZZZZ

As Lloyd said, you will have to do it one node at a time, but I suspect the numbers indicate a position in the tree. With all items having just tree characters in the number equals root nodes.

If the list is ordered you should be able to do it recursively. If not, it may be worthwhile to try to sort it before traversing the list.

An rough idea for a recursive loop, might be something like this (using an Arraylist for the data). Note that this code does not take into account any numbering and assumes items are sorted.

private void FillTree(ArrayList list)
{
TreeNode current = null;
for (int i = 0; i < list.Count;)
{
string item = (string)list;
string num = item.Split('-')[0].Trim();
string name = item.Split('-')[1].Trim();
if (num.Length == 3)
{
current = treeView1.Nodes.Add(name);
i++;
}
else
i = AddNode(current, 6, i, list);
}
}

private int AddNode(TreeNode node, int len, int index, ArrayList list)
{
string item = (string)list[index];
string num = item.Split('-')[0].Trim();
string name = item.Split('-')[1].Trim();
TreeNode current = node.Nodes.Add(name);
index++;
for (int i = index; i < list.Count;)
{
item = (string)list[index];
num = item.Split('-')[0].Trim();
name = item.Split('-')[1].Trim();
if (num.Length > len)
index = AddNode(current, len + 3, index, list);
else
return index;
}
return index;

}
 

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

Similar Threads


Top