Results 1 to 3 of 3

Thread: Treeview fun!

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    6

    Treeview fun!

    Hi,

    I am trying to dynamically build a multi level tree view tree for data held in an SQL database. The database holds the node name and the parent name, this allows for a flexible number of levels to be stored.

    The issue I'm having is translating the level structure to the treeview component. I can recursively query the database but I can't programatically select a treenode in order to add a level.
    I have tried using:

    Code:
    treeView1.SelectedNode=treeView1.Nodes.Find("Node1", true)
    or

    Code:
    Dim node As TreeNode() = TreeView1.Nodes.Find("<selected word>", True)
    TreeView1.SelectedNode = node(0)
    and every combination of that I can find in forums but each time nothing is found so the selectednode assignment fails.

    Any points would be greatly appreciated!

  2. #2

    Thread Starter
    New Member
    Join Date
    Oct 2017
    Posts
    6

    Re: Treeview fun!

    I think I might have got it.

    When adding the parent node to the treeview I was only adding the text and not the key. By specifiing the and the name:

    Code:
    parent="Top Level"
    TreeView1.Nodes.Add(parent, parent)
    Code:
     Dim myNodes() As TreeNode = TreeView1.Nodes.Find(parent, True)
                If Not sqldatareader.Item(0) Is DBNull.Value Then
    
                    myNodes(0).Nodes.Add(sqldatareader.Item(0).ToString, sqldatareader.Item(0).ToString)
    
                End If
    This seems to now dynamically build the correct tree structure.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Treeview fun!

    Quote Originally Posted by Mattpstanding View Post
    I think I might have got it.

    When adding the parent node to the treeview I was only adding the text and not the key. By specifiing the and the name:

    Code:
    parent="Top Level"
    TreeView1.Nodes.Add(parent, parent)
    Code:
     Dim myNodes() As TreeNode = TreeView1.Nodes.Find(parent, True)
                If Not sqldatareader.Item(0) Is DBNull.Value Then
    
                    myNodes(0).Nodes.Add(sqldatareader.Item(0).ToString, sqldatareader.Item(0).ToString)
    
                End If
    This seems to now dynamically build the correct tree structure.
    You're right. That's how to do exactly what you asked. No trickery, simply a case of using a key on each node...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width