Populating treeview with sqlite table using recursive function

Discussion in 'Questions (Windows Mobile)' started by tinfoil, Mar 29, 2009.

  1. tinfoil

    tinfoil New Member


    I'm trying to load a treeview with data from a sqlite table. The table contains heirarchial data using the adjacency model.
    For example, if the table contained an organisational structure it would be as follows.

    Col1: EmployeeName
    Col2: Boss


    In the treeview, this would be displayed as follows


    I have done this in vb6 before using a recursive procedure call, i.e. You first find the parent record (the one with a null boss) and add it to the treeview, you then filter all records that have that boss and add them to the treeview. After adding each node, you recall the prodedure passing the current employee as the filter (boss) for the next level in the treeview.

    My problem is that I cannot see how I can pass the current node within the recursive sub so the employees go under the correct branch of the tree.

    Many thanks
    Last edited: Mar 29, 2009
  2. specci48

    specci48 Well-Known Member Licensed User

    Hi tinfoil,

    here is a small sample populating a treeview in a recursive way (with numbers form 0 to 8).
    Tree1 is a treeview object and node1/node2 are node objects.

    Sub Globals
    'Declare the global variables here.
       a = 0
    End Sub

    Sub App_Start
       node1.Value = tree1.AddNewNode(a)
    End Sub

    Sub FillRecursive
    If a < 8 Then
          a = a + 
          node2.Value = node1.AddNewNode(a)
          node1.Value = node2.Value
    End If
    End Sub
    The root node is added before the recursion because you have to work with a treeview, not a node.

  3. tinfoil

    tinfoil New Member

    Thank you Specci48

    Unfortunately, your solution does not give me quite what I'm after. The tree will be structured as:

    I think this is because the node1 value is global and is not maintained within each recursion of the loop.
    For example, in the example above, when Carl has been added to the tv, that sub will end and the sub that created Carl (Tim) will continue and so node1 will need to be set to Tim and not Carl. My problem is that I require infinate levels and so I do not wish to have a node defined for each level.

  4. agraham

    agraham Expert Licensed User

    Try resetting the node to its parent after the return from FillRecursive.

    Sub FillRecursive
    ' add node
    node = node.Parent
    End Sub
  5. tinfoil

    tinfoil New Member

    Sample app showing problem

    Hi All

    I have added the source code that shows my problem The problem is commented in sub sbRecurseArray


    Attached Files:

  6. tinfoil

    tinfoil New Member

    Thanks everyone for all your help. I have added agraham's code snippet to the bottom of the recursive sub and bingo.

    The working code now is:

    Sub sbRecurseArray(lTriggerID)

    Dim i

    For i = 0 To Table1.RowCount -1

    If Table1.Cell("NodeParentID",i) = lTriggerID Then
    'Msgbox (Table1.Cell("NodeName",i))
    node2.Value = node1.AddNewNode(Table1.Cell("NodeName",i))

    node1.Value = node2.Value

    End If
    Next i
    node1.value = node1.parent

    End Sub

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice