473,473 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

VB.Net 2005 Treeview example required

Hi all this may seem really easy to most of you, but Im just getting to
grips with 2005 and the new features.

I have a dataset from a database which has a ParentID, ChildID and
ObjectName fields.
I was under the impression I should be able to just cycle through the
rows in the dataset and add them (or insert them using the indexofkey
by the ParentID) to the TreeNodeCollection for the treeview. However,
when I go to fins the indexs for the third level objects or lower no
index is found and it all goes pear shaped.

If anyone has a very simple example of populating a treeview control
using vb.net from a dataset using the schema I have described, please
point me in the right direction or email it to me at karlrhodes at
hotmail dot com
Dim arrNodes() As TreeNode
Dim oNodes As TreeNodeCollection = myTreeView.Nodes
For Each oDRow In myDataSet.Tables("Hierarchy").Rows
strChildID = oDRow("Object_Id")
strObjectName = oDRow("ObjectName")
If Not IsDBNull(oDRow("Parent_Id")) Then strParentID =
oDRow("Parent_Id") Else strParentID = 0
arrNodes = oNodes.Find(strParentID, True)
numX = UBound(arrNodes)
If numX >= 0 Then
intIndex = arrNodes(numX).Index
Else
intIndex = -1
End If
If numX = 0 Then
oNodes.Add(strChildID, strObjectName)
Else
If oNodes.ContainsKey(strParentID) Then
oNodes(intIndex).Nodes.Add(strChildID, strObjectName)
End If
End If
numX = numX + 1
Next
Thanks

Mar 7 '06 #1
5 15163
As is often the case, Ive tidied the code up below and it doesnt work
But Im sure you get the idea. :)

Mar 7 '06 #2
"Karl Rhodes" <go**********@tlbsolutions.com> schrieb
If anyone has a very simple example of populating a treeview control
using vb.net from a dataset using the schema I have described,
please point me in the right direction or email it to me at
karlrhodes at hotmail dot com


Put a Treeview and a Button on a new WindowsApplication. Insert the
following code. As you see, filling the Treeview is the smaller part.

Private Sub Button1_Click( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button1.Click

Dim ds As New DataSet
Dim dt As DataTable = ds.Tables.Add

dt.Columns.Add("id", GetType(Integer))
dt.Columns.Add("idparent", GetType(Integer))
dt.Columns.Add("text", GetType(String))

dt.Rows.Add(New Object() {1I, DBNull.Value, "node1"})
dt.Rows.Add(New Object() {2I, 1I, "node2"})
dt.Rows.Add(New Object() {3I, 1I, "node3"})
dt.Rows.Add(New Object() {4I, 2I, "node4"})
dt.Rows.Add(New Object() {5I, 2I, "node5"})
dt.Rows.Add(New Object() {6I, DBNull.Value, "node6"})
dt.Rows.Add(New Object() {7I, 6I, "node7"})

AddNodes(dt, dt.Select("isnull(idparent, -1) = -1"), TreeView1.Nodes)

End Sub

Private Sub AddNodes( _
ByVal dt As DataTable, ByVal Rows As DataRow(), _
ByVal Nodes As TreeNodeCollection)

For Each row As DataRow In Rows
Dim node As TreeNode
Dim SubRows() As DataRow

node = Nodes.Add(row(2).ToString)
SubRows = dt.Select("idparent = " & DirectCast(row(0), Integer))
AddNodes(dt, SubRows, node.Nodes)
Next

End Sub

Armin

Mar 7 '06 #3
Armin,

What a star you are! It works perfectly!

Many, many thanks!

Karl

Mar 7 '06 #4
Armin,

Can you just explain the lines that read

AddNodes(dt, dt.Select("isnull(idparent, -1) = -1"), TreeView1.Nodes)

and

SubRows = dt.Select("idparent = " & DirectCast(row(0), Integer))
Im not sure I understand how the 'Select' part of each line works.

I am guessing that in the second of the 2 lines
SubRows = dt.Select("idparent = " & DirectCast(row(0), Integer))
is doing a select for all records from the datatable where the idparent
= the value of the first row as an integer?

I have no clue at all what "isnull(idparent, -1) = -1" does.

Mar 7 '06 #5
"Karl Rhodes" <go**********@tlbsolutions.com> schrieb
Armin,

Can you just explain the lines that read

AddNodes(dt, dt.Select("isnull(idparent, -1) = -1"),
TreeView1.Nodes)

and

SubRows = dt.Select("idparent = " & DirectCast(row(0), Integer))
Im not sure I understand how the 'Select' part of each line works.

I am guessing that in the second of the 2 lines
SubRows = dt.Select("idparent = " & DirectCast(row(0), Integer)) is
doing a select for all records from the datatable where the idparent
= the value of the first row as an integer?

I have no clue at all what "isnull(idparent, -1) = -1" does.


According to

http://msdn.microsoft.com/library/en...ssionTopic.asp

IsNull converts the value to the replacment value, -1 in this case, if the
field value is Null. It's equal to "idparent is null" in an SQL query. Be
aware that there must never be -1 in the database in this field, because
the comparison doesn't distinguish between Null and -1. As a whole, the
first Select method returns all rows with IDParent=Null. These rows are the
top-level rows, i.e. the rows that do not have a parent.

The second call to the Select method finds the child rows of the row that
has just been added. The child rows are those whose IDparent is equal to the
ID of the added row. The function is called recursively then to do the same
for these rows. And so on.
Armin

Mar 7 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: T.H.M | last post by:
This is the all code. Very simple and short. I need to populate a treeView in aspx page (web form) from XML file. I get en completion error:No overload for method 'TreeNode' takes '1' arguments ....
42
by: lauren quantrell | last post by:
So many postings on not to use the treeview control, but nothing recently. Is it safe to swim there yet with Access 2000-Access 2003?
6
by: Brian Smith | last post by:
Is there a way to avoid the default action of TreeNode expansion/contraction caused by double click? I can add an event handler to pop up my properties dialog on double click, but it has the...
4
by: pmcguire | last post by:
I have a treeview with a lot of nodes. I want to load only the nodes that are initially visible when the form loads, and then continue to populate it in background and/or when the nodes are required...
0
by: K B | last post by:
Hi, I can find lots of references in using Asp.Net 2.0 treeview to bind to more simple xml, but not that shows how to use it with elements AND attributes. For example, here is the type of...
1
by: Daves | last post by:
Sorry folks but I will be reposting this question from 6/5 until someone gives me an answer - the question is very easy and so ought the answer to be. I really need the answer! ------- I'm...
2
by: metaperl | last post by:
Hello, I'm trying to get the MSDN documentation example of a treeview to work: http://msdn2.microsoft.com/en-us/library/system.windows.forms.treeview.aspx I made the function static and added a...
26
by: JJ | last post by:
Is there any way you can expand a parent node on the treeview control _without a postback_ by clicking on the node text (NOT clicking the expand image URL) ? I want to format the treeview node...
2
by: Luqman | last post by:
I have filled a treeview with parents and childs, using Hashtables. Now, how can I check the required Parent and its Child Nodes. for example, I have following data Group Item A Item A1...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.