I been try to fix this problem, in code but until now i don't know how to get the correct result this is my table
+--------+-----------+----------+
| id | name | node_id |
+--------+-----------+-----------
| 1 | Color | NULL |
| 2 | Black | 1 |
| 3 | Red | 1 |
| 4 | White | 1 |
| 5 | Animal | NULL |
| 6 | Dog | 5 |
| 7 | Cat | 5 |
+--------+-----------+----------+
I want to display this in my Form in Tree view like this, but it was messed up as I expected. The Color, Animal is the Parent node, and Black , White, Dog, Cat is the child node
Color
|-Black
|-Red
Animal
|-Dog
|-Cat
This is the code for the tree view using VB.NET:
'select the min Id as the starting of node
Dim SQL As String = "select min(node_id), name from mytable;"
Dim cmd As MySqlCommand = New MySqlCommand(SQL, cn)
cmd.ExecuteNonQuery()
Dim reader As MySqlDataReader = cmd.ExecuteReader()
Dim id As Integer
Dim name As String
Dim node1, node2 As TreeNode
While reader.Read
id = reader.GetString(0)
name = reader.GetString(1)
End While
reader.Close()
cmd.CommandText = "select id, name from mytable"
cmd.ExecuteNonQuery()
Dim ds As New DataSet
Dim da As New MySqlDataAdapter
'add the first find of min id = name
node1 = TreeView1.Nodes.Add(id, name)
If node1 is Nothing Then
node2 = TreeView1.Nodes.Add(id, name)
Else
node2 = node1.Nodes.Add(id, name)
End If
Dim dr = cmd.ExecuteReader
Do While dr.Read()
TreeView1.Nodes.Add(dr("id"), dr("name"))
TreeView1.Nodes(node2.Level).Nodes.Add(dr("id"), dr("name"))
Loop
dr.Close()
