PC Review Forums Newsgroups Microsoft DotNet Microsoft Dot NET Framework Forms Filling a treeview

Reply

Filling a treeview

 
Thread Tools Rate Thread
Old 11-02-2006, 10:56 PM   #1
MauriZZZZ
Guest
 
Posts: n/a
Default Filling a treeview


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

  Reply With Quote
Old 12-02-2006, 12:10 AM   #2
Lloyd Dupont
Guest
 
Posts: n/a
Default Re: Filling a treeview

> 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?


  Reply With Quote
Old 12-02-2006, 01:37 PM   #3
Morten Wennevik
Guest
 
Posts: n/a
Default Re: Filling a treeview

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[i];
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;

}

--
Happy coding!
Morten Wennevik [C# MVP]
  Reply With Quote
Old 12-02-2006, 09:06 PM   #4
MauriZZZZ
Guest
 
Posts: n/a
Default Re: Filling a treeview

Morten, you are the man! Recursive functions are not my thing... Thanks!

  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off