Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Possible help me in TreeView1 tool vb net
I have a table in the database like
--------------------------------------------
ID   |    SNAme  | IDParent   
--------------------------------------------
1    |   N1      |  null
2    |   N2      |  null
3    |   N11     |  1
4    |   N22     |  2
5    |   N111    |  3
6    |   N222    |  4
7    |   N1111   |  5
---------------------------------------------

i need Display data like :
1- N1
----3-N11
------5-N111
---------7- N1111
2- N2
---4-N22
------6-N222


-Update
VB
SQLTemp = "SELECT TbAccF.* FROM dbo.TbAccF "
cmd = New SqlCommand(SQLTemp, Conn)

If Conn.State = ConnectionState.Open Then Conn.Close()
Conn.Open()
dr = cmd.ExecuteReader

Dim dt As New DataTable("TbAccF")
dt.Load(dr)
MsgBox(dt.Rows.Count)
For Each dr As DataRow In dt.Rows
    AddNode(dr("ParentAccID").ToString, dr("AccNameAr").ToString)
Next
End Sub

Private Sub AddNode(parentNode As String, nodeText As String)
Dim node As New List(Of TreeNode)
node.AddRange(TreeView1.Nodes.Find(parentNode, True))
If Not node.Any Then
    node.Add(TreeView1.Nodes.Add(parentNode, parentNode))
End If
node(0).Nodes.Add(nodeText, nodeText)
End Sub
Posted
Updated 7-Aug-15 12:35pm
v3
Comments
PIEBALDconsult 7-Aug-15 18:11pm    
Sure, go ahead, it's easy.
One hint I'll give is to have a Dictionary<int,treenode> to help speed up the search for parent nodes.
spotligh_ly 7-Aug-15 18:29pm    
I know my brother, it is very easy!!!! ...
But sometimes when you're in a bad mood simplest things become the most complex
PIEBALDconsult 7-Aug-15 18:51pm    
VB will do that to you.

Also, please stop using ToString so much (and don't use Convert either).
Patrice T 7-Aug-15 18:17pm    
Show us what you have done so far.
Tell us where is your difficulty.
spotligh_ly 7-Aug-15 18:25pm    
SQLTemp = "SELECT TbAccF.* FROM dbo.TbAccF "
cmd = New SqlCommand(SQLTemp, Conn)

If Conn.State = ConnectionState.Open Then Conn.Close()
Conn.Open()
dr = cmd.ExecuteReader

Dim dt As New DataTable("TbAccF")
dt.Load(dr)
MsgBox(dt.Rows.Count)
For Each dr As DataRow In dt.Rows
AddNode(dr("ParentAccID").ToString, dr("AccNameAr").ToString)
Next
End Sub
Private Sub AddNode(parentNode As String, nodeText As String)
Dim node As New List(Of TreeNode)
node.AddRange(TreeView1.Nodes.Find(parentNode, True))
If Not node.Any Then
node.Add(TreeView1.Nodes.Add(parentNode, parentNode))
End If
node(0).Nodes.Add(nodeText, nodeText)
End Sub

1 solution

tn-ks problem has been resolved.....



VB
SQLTemp = "SELECT TbAccF.* FROM dbo.TbAccF "
    cmd = New SqlCommand(SQLTemp, Conn)
    If Conn.State = ConnectionState.Open Then Conn.Close()
    Conn.Open()
    dr = cmd.ExecuteReader

    Dim dt As New DataTable("data")
    dt.Load(dr)
    Dim ds As New DataSet()
    ds.Tables.Add(dt)
    ds.Relations.Add("rsParentChild", ds.Tables("data").Columns("id"), ds.Tables("data").Columns("ParentId"))
    For Each dr As DataRow In ds.Tables("data").Rows
        If dr("ParentId") Is DBNull.Value Then
            Dim root As New TreeNode(dr("AccNameAr").ToString())
            root.Tag = dr("CodeID").ToString()
            TreeView1.Nodes.Add(root)
            PopulateTree(dr, root)
        End If
    Next
    TreeView1.ExpandAll()
End Sub

Public Sub PopulateTree(dr As DataRow, pNode As TreeNode)
    For Each row As DataRow In dr.GetChildRows("rsParentChild")
        Dim cChild As New TreeNode(row("CodeID").ToString() & " : " & row("AccNameAr").ToString())
        cChild.Tag = row("CodeID").ToString()
        pNode.Nodes.Add(cChild)
        PopulateTree(row, cChild)
    Next
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900