dcsimg
Results 1 to 5 of 5

Thread: [RESOLVED] Pupulating a TreeView with childnode based on sql data

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2018
    Posts
    364

    Resolved [RESOLVED] Pupulating a TreeView with childnode based on sql data

    New challenge 😊
    On form_load event, I need to populate a treeview with all items from a sql table that have status “New”. The childnode of that node (let’s call it “Inbox”) shall contain all the items that have status “new”.

    So the situation can be depicted as:

    A: Either no new item at this time:
    Inbox (0)

    B: There are e.g. 5 items with status “New”
    + Inbox (5)
    And by clicking the plus sign:

    -Inbox (5)
    Item 00001
    Item 00002
    Item 00003
    Item 00004
    Item 00005


    I can get the data from sql table but then I am stuck ☹ I could also use other controls if it is easier but I like the in and out folding of the treeview control for this purpose. I can also create the parrent node but then have no idea how to take the next step.

    Thanks again for help.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2018
    Posts
    364

    Re: Pupulating a TreeView with childnode based on sql data

    OK, I went ahead and cooked some code:

    Code:
     Dim NewItems As DataRow() = FullTable.Select("Request_Phase = 'New'")
       Me.TreeView1.Nodes.Add("Inbox (" & NewItems.Count & ")")
        If NewItems.Count > 0 Then
            Dim MyNode() As TreeNode
            MyNode = Me.TreeView1.Nodes.Find("Inbox", True)
            For Each Drow As DataRow In NewItems '
                MyNode(0).Nodes.Add(Drow.Item("Request_ID").ToString)’this line gives me the error
            Next Drow
         End If
    But it give me this error “Index was outside the bounds of the array”
    What am doing wrong? All of it is wrong? Or just some of it? How do I add those children under Inbox node?

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,238

    Re: Pupulating a TreeView with childnode based on sql data

    I haven't used a TreeView for a long time, but if memory serves this is the way to do it:
    Code:
     Dim NewItems As DataRow() = FullTable.Select("Request_Phase = 'New'")
       Dim MyNode As TreeNode = Me.TreeView1.Nodes.Add("Inbox (" & NewItems.Count & ")")
        If NewItems.Count > 0 Then
            For Each Drow As DataRow In NewItems '
                MyNode.Nodes.Add(Drow.Item("Request_ID").ToString)’this line gives me the error
            Next Drow
         End If
    (and as the only thing inside the If block now is the ForEach loop, you don't need the If)

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2018
    Posts
    364

    Re: Pupulating a TreeView with childnode based on sql data

    OMG, it is working, it is working, it is working Thank so much. And thanks for pointing out the redundant IF thing.
    Last edited by Grand; Dec 3rd, 2018 at 02:19 AM.

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,238

    Re: Pupulating a TreeView with childnode based on sql data

    No worries


    As you now have it sorted out, could you please do us a little favour, and mark the thread as Resolved?
    (this saves time reading for those of us who like to answer questions, and also helps those who search to find answers)

    You can do it by clicking on "Thread tools" just above the first post in this thread, then "Mark thread resolved". (like various other features of this site, you need JavaScript enabled in your browser for this to work).

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width