1

I have many records in a database and I need to populate my treeview dynamically like this: Below is just an example of what I need:

TreeView1.Nodes(a).ChildNodes.Add(New TreeNode("ChildNode " & b))
TreeView1.Nodes(a).ChildNodes(b).ChildNodes.Add(New TreeNode("ChildNode 2 lvl " & b))

I'm getting the records from a MySQL Db and I need to know how can I add multilevel ChildNodes into a loop For ... Next etc...

Do you have any suggestion or idea???

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ecusad
  • 13
  • 6

1 Answers1

0

if you want to work with various levels of Treenodes you can use Find function

Dim TempNode As TreeNode = TreeView1.Nodes.Find("Node where I want to add SubNode", True).FirstOrDefault

TempNode.Nodes.Add("SubNode", "SubNode")

This way you can add SubNode to any Node you pick.

.Find("key",True)finds treenodes with following key and .FirstOrDefault picks first. Finally you just add new SubNode to Tempnode.

You considered you are getting it dynamically and from MySql. It may cause error like "Action beeing preformed on this control is being called from the wrong thread. Marshal to the correct thread using Contol.Invoke or Control.BeginInvoke to perform this action." Simply change TempNode.Nodes.Add("SubNode", "SubNode") to TreeView1.Invoke(Sub() TempNode.Nodes.Add("SubNode", "SubNode"))

EXAMPLE:

Dim comm As String = "SELECT * FROM YourTableName"
Dim SqlCmnd as SqlCommand = New SqlCommand(comm, YourMySqlConnection)
Dim READER As SqlDataReader
READER = SqlCmnd.ExecuteReader
While READER.Read
    Dim NewNode As TreeNode = New TreeNode(READER.Item("origCategoryID"))
    TreeView1.Nodes.Add(NewNode)
    NewNode.Nodes.Add(READER.Item("categoryOrderID"))
End While
READER.Close()

EXAMPLE 2:

output database

While READER.Read
If TreeView1.Nodes.Find(READER.Item("OrigCatOrderID"), True).Length > 0 Then
Dim NewNode As TreeNode = TreeView1.Nodes.Find(READER.Item("OrigCatOrderID"), True).FirstOrDefault
NewNode.Nodes.Add(READER.Item("CatOrderID"), READER.Item("CatOrderID"))
Else
TreeView1.Nodes.Add(READER.Item("OrigCatOrderID"), READER.Item("OrigCatOrderID"))
TreeView1.Nodes(READER.Item("OrigCatOrderID")).Nodes.Add(READER.Item("CatOrderID"), READER.Item("CatOrderID"))
End While
megy
  • 16
  • 4
  • Thanks for your help!! Can I ask you to write down just an example ??? I'm not familiar with this component! – Ecusad Aug 19 '20 at 14:13
  • @Ecusad Can you specify structure of your database to write right example ? – megy Aug 20 '20 at 21:52
  • I have 3 columns - "OrigCategoryOrderID" "CategoryOrderID" and "CategoryName". In the First I keep the actual NodeID, In the Second I keep the ChildNodeID. I'll make an example: `Node1(origCategoryID) - ChildNode1(categoryOrderID) or Node1 - Node1a(OrigCategoryOrderID) - ChildNode1a(categoryOrderID)` ... I hope you understanding what I mean – Ecusad Aug 21 '20 at 14:27
  • I wrote example with Sql database not MySql, but in fact it's same. I hope you don't mind. I suppose you know to work with SQL so I didn't write the whole code (declaring SQL connection, opening,...) just main part. Example is in edited answer, if you need specify something in example or add any feature write me. – megy Aug 23 '20 at 16:00
  • Thanks for your help!!! I'm trying to apply what you are suggesting in my code and when I'll done I'll let you know!!!! Just in case can you make some example with the ChildNodes??? How can I handle a routine with more sub level of ChildNodes?? – Ecusad Aug 24 '20 at 14:25
  • I made one more example. I'm not sure about ChildNodes. What version of Visual Studio are you using and which Framework ? – megy Aug 25 '20 at 10:34
  • I don't know then what do you meant with "Find is not a member of TreeNodeCollection!"link:https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.treenodecollection.find?view=netcore-3.1 – megy Aug 27 '20 at 20:13
  • I don't know what can I say, btw I fixed with FindNode(). – Ecusad Aug 27 '20 at 21:11