5 Replies - 225 Views - Last Post: 03 December 2018 - 04:55 AM Rate Topic: -----

#1 Grander   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 15-November 18

Pupulating a TreeView with childnodes based on sql data

Posted 02 December 2018 - 12:37 PM

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.
Here is my "code" so far but it gives an error:“Index was outside the bounds of the array”

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"))’this line gives me the error
        Next Drow
     End If


Thanks again for help

This post has been edited by Grander: 02 December 2018 - 12:38 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Pupulating a TreeView with childnodes based on sql data

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14691
  • View blog
  • Posts: 58,718
  • Joined: 12-June 08

Re: Pupulating a TreeView with childnodes based on sql data

Posted 02 December 2018 - 01:24 PM

You know there is at least one node in the tree from this line.
2	   Me.TreeView1.Nodes.Add("Inbox (" & NewItems.Count & ")")


So there's no need to 'find' it. Just reference it as the 0 node.
5	        MyNode = Me.TreeView1.Nodes.Find("Inbox", True)


.. and add things to it.

        TreeView1.Nodes(0).Nodes.Add("asdf")
        TreeView1.Nodes(0).Nodes.Add("hgj")
        TreeView1.Nodes(0).Nodes.Add("1234")
        TreeView1.Nodes(0).Nodes.Add("asw2er")



This is assuming 'Drow.Item("Request_ID")' actually is an item in that object.
Was This Post Helpful? 0
  • +
  • -

#3 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 171
  • View blog
  • Posts: 1,026
  • Joined: 05-December 13

Re: Pupulating a TreeView with childnodes based on sql data

Posted 02 December 2018 - 07:17 PM

When you start righting your code in a structured way, you will find it much easier to do what you want to do, especially if you make your code reusable. Well I see Modi has shown you how child nodes get added to the parents, and while you might be looking for something more. Perhaps this will help you out.

Console output ::
The first node :: Inbox didn't exist, but has now been created.
We found and added 1 new items to the treeview
We found and added 2 new items to the treeview
We found and added 3 new items to the treeview
The second node :: Old didn't exist, but has now been created.


Attached Image

Works perfectly as seen in screenshot

This is where the magic happens. Now remember this is just POC code (Proof of concept) , and is not to be taken as is. You will need to edit in how you pass your SQL data to it. Since I didn't have a SQL server, I made some makeshift data out of a dictionary and a class which you can remove called FromSQLServer
Spoiler

This is the code below where i made some makeshift values to loop through, as you would when receiving data with a stream reader. Easy to change

This is just a junk class i made so I had data to work with to create the nodes. ::
Spoiler

Complete project, dump this code into a new form, add a TreeView and start debugging. I'm sure it will help you to understand what you need to do.
Spoiler


Really tired here, so off to bed i go, hope you find the code useful. I'll be back on tomorrow if you have questions.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14691
  • View blog
  • Posts: 58,718
  • Joined: 12-June 08

Re: Pupulating a TreeView with childnodes based on sql data

Posted 02 December 2018 - 10:36 PM

Let's ease up on doing folk's work for them.


As an aside - I don't get the point of rewriting the existing treeview functions. :unsure:
Was This Post Helpful? 0
  • +
  • -

#5 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 171
  • View blog
  • Posts: 1,026
  • Joined: 05-December 13

Re: Pupulating a TreeView with childnodes based on sql data

Posted 03 December 2018 - 03:15 AM

I consider mo posts demonstrated, and so they learn from examples I share. Op still has to add their SQL to my class. I'm not doing all the work, where else am I doing others work for them?
Was This Post Helpful? 0
  • +
  • -

#6 Grander   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 15-November 18

Re: Pupulating a TreeView with childnodes based on sql data

Posted 03 December 2018 - 04:55 AM

Hey,
Thanks for the guidance.

These simple lines solved my issue:

   Dim NewItems As DataRow() = FullTable.Select("Request_Phase = 'New'")'full table is a datatable imported from sql
   Dim MyNode As TreeNode = Me.TreeView1.Nodes.Add("Inbox (" & NewItems.Count & ")")
    For Each Drow As DataRow In NewItems '3
       MyNode.Nodes.Add("JFIM " & Drow.Item("Request_ID")) 'this line gives me the error
    Next Drow


This post has been edited by Grander: 03 December 2018 - 04:56 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1