0

I have been given a dataset to work with that is layed out in the format of

Object | Type | Level | Comments | Parent | Child

So far I have been able to get the Object as the Parent Node and the Comments as a child node, however I need to get multiple children for this parent, and then children on them

An example of what I mean is like so

Object | Type | Level | Comments | Parent | Child
Dave   | WH   | 1     | comment  | root   | null
Simon  | WH   | 1     | comment  | root   | Fortnum
Simon  | WH   | 1     | comment  | root   | Mason
Tim    | WH   | 1     | comment  | root   | null
wallace| WH   | 2     | comment  | Simon  | null
Mason  | WH   | 2     | comment  | Simon  | Mouse
Mouse  | WH   | 3     | comment  | Mason  | null

I need it to look like this

enter image description here

I have looked at the code from here Similar Stack Answer but its not working for me

I am pulling the sql data into a datatable and then looping through it to try and build the tree view.

This is the code that I am using that is only giving me the object as a parent node, and then the comment as a child node, but I need to be able to locate the actual children of the and then add them to the treeview.

        For Each row As DataRow In dt.Rows
        node = Searchnode(row.Item(4).ToString(), TreeView1)
        If node IsNot Nothing Then
            subNode = New TreeNode(row.Item(3).ToString())
            node.ChildNodes.Add(subNode)
        Else
            node = New TreeNode(row.Item(0).ToString())
            subNode = New TreeNode(row.Item(3).ToString())
            node.ChildNodes.Add(subNode)
            TreeView1.Nodes.Add(node)
        End If
     Next

then the function

Private Function Searchnode(ByVal nodetext As String, ByVal trv As TreeView) As TreeNode
    For Each node As TreeNode In trv.Nodes
        If node.Text = nodetext Then
            Return node
        End If
    Next
End Function

Ive never really worked with treeviews before in ASP.Net but would be very grateful if anyone can help me.

Community
  • 1
  • 1
Simon Price
  • 3,011
  • 3
  • 34
  • 98

1 Answers1

-1

Its not necessary to add child nodes when you add a new node. If the datatable is sorted the way your example shows the childnode will come up with a reference to its parent after the parent has already been added to the treeview. so prior to this example edit the datatable and remove the duplicate objects like remove one of the simons. the(child) column is not needed. also (optionally) going to add a reference to the parent when adding the child.

For Each row As DataRow In dt.Rows
    node = Searchnode(row.Item(4).ToString(), TreeView1.Nodes)
    If node IsNot Nothing Then
        subNode = New TreeNode(row.Item(0).ToString()){ parent= node }
        node.Nodes.Add(subNode)
    Else 'root node
        node = New TreeNode(row.Item(0).ToString())
        TreeView1.Nodes.Add(node)
    End If
Next

also searchnode needs to be recursive to check child nodes.

Private Function Searchnode(ByVal nodetext As String, ByVal tn As TreeNodeCollection) As TreeNode
    TreeNode ret = nothing
    For Each node As TreeNode In tn.Nodes
        If node.Text = nodetext Then
            ret = node
            exit for
        Else 
           ret = Searchnode(nodetext, node.Nodes)
        End If
    Next
    Return ret
End Function
Vernard Sloggett
  • 336
  • 4
  • 10
  • Thanks for the response, i will test this with my data tomorrow and get back to you – Simon Price Nov 11 '15 at 23:00
  • @Verad Sloggett I am finding problems with your solution `()){ parent= node }` gives and error ` "Operator '=' os not defiened for types 'System.Web.UK.Control' and "System.Web.UK.WebControls.TreeviewNode' `Also `node.Nodes` `'Nodes is not a member of 'System.Web.UK.WebControls.TreeNode'"`. The in the function `tn.Nodes` `""System.Web.UK.WebControls.TreeviewNode' Also `node.Nodes` 'Nodes is not a member of 'System.Web.UK.WebControls.TreeNodeCollection"` – Simon Price Nov 12 '15 at 10:42
  • `changing node.Nodes.Add` to `node.ChildNodes.Add` appears to fix this but I still have the issue on `{Parent = Node}` and `tn.Nodes` – Simon Price Nov 12 '15 at 10:44