Home All Groups Group Topic Archive Search About

VB.Net 2005 Treeview example required

Author
7 Mar 2006 2:58 PM
Karl Rhodes
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

Author
7 Mar 2006 4:20 PM
Karl Rhodes
As is often the case, Ive tidied the code up below and it doesnt work
But Im sure you get the idea. :)
Author
7 Mar 2006 4:25 PM
Armin Zingler
"Karl Rhodes" <googlegro***@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
Author
7 Mar 2006 5:06 PM
Karl Rhodes
Armin,

What a star you are! It works perfectly!

Many, many thanks!

Karl
Author
7 Mar 2006 5:14 PM
Karl Rhodes
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.
Author
7 Mar 2006 6:42 PM
Armin Zingler
Show quote Hide quote
"Karl Rhodes" <googlegro***@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-us/cpref/html/frlrfSystemDataDataColumnClassExpressionTopic.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