PC Review
Forums
Newsgroups
Microsoft DotNet
Microsoft Dot NET Framework Forms
Filling a treeview
Forums
Newsgroups
Microsoft DotNet
Microsoft Dot NET Framework Forms
Filling a treeview
![]() |
Filling a treeview |
|
|
Thread Tools | Rate Thread |
|
|
#1 |
|
Guest
Posts: n/a
|
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 |
|
|
|
#2 |
|
Guest
Posts: n/a
|
> 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? |
|
|
|
#3 |
|
Guest
Posts: n/a
|
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] |
|
|
|
#4 |
|
Guest
Posts: n/a
|
Morten, you are the man! Recursive functions are not my thing... Thanks!
|
|
![]() |
|
| Thread Tools | |
| Rate This Thread | |
|
|

Main Page 


