Link to home
Start Free TrialLog in
Avatar of Altaf Khan
Altaf Khan

asked on

How to create tree view with sub view

Hi
I am trying to create tree view and so far my progress is as follow..
but not as i expected ..
my code is as follow
    With RsHLList
        Do While Not .EOF
            TreeView1.Style = tvwTreelinesPlusMinusText
            
            With TreeView1.Nodes.Add(, , , RsHLList(0), 1)
                TreeView1.Nodes.Add .Index, tvwChild, , RsHLList(1), 2
                With TreeView1.Nodes.Add(.Index, tvwChild, , RsHLList(2), 2)
                    .Expanded = True
                End With
                .Expanded = True
            End With
            .MoveNext
        Loop
        .Close
    End With

Open in new window


i want to tree view like
ALTAF
    50
              10
              12
              18
ALTAF
     100
            14
            22
            26
            35
JHON
     250
            25
            30
EE_treeview.jpg
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Altaf Khan
Altaf Khan

ASKER

thanks for reply..
let me try
Getting run time error 13 - type mismatch

and highlighted on
               Set node_root = TreeView1.Nodes.Add(, , key_root, RsHLList(0), 1)
Getting run time error 13 - type mismatch

have you declared the object properly?

Dim node_root As Node

Open in new window

yes .. please check following code



 Dim RsHLList As New ADODB.Recordset

   Dim cnt As Integer, tmpidx As Integer, i As Integer
    
    Dim node_root As Node
    Dim node_2nd As Node
    Dim node_3rd As Node
    Dim key_root As String
    Dim key_2nd As String
    Dim key_3rd As String

Set RsHLList = Nothing
If RsHLList.State = 1 Then RsHLList.Close
        
    RsHLList.Open "SELECT Distinct(EmpName), LoanAMT, RCVD_Amount  FROM HandLoan_DTL where Loan_ON_OFF = 'ON' order by EmpName", Con_Main, adOpenKeyset, adLockOptimistic
    
    
    TreeView1.Style = tvwTreelinesPlusMinusText
    
    With RsHLList
        Do While Not .EOF
            cnt = cnt + 1
            
            If tmpidx = RsHLList(1) Then
                Set node_3rd = TreeView1.Nodes.Add(key_2nd, tvwChild, key_3rd & cnt, RsHLList(2), 1)
            Else
                key_root = "root" & cnt
                key_2nd = "node_2nd" & cnt
                key_3rd = "node_3rd" & cnt
                
                Set node_root = TreeView1.Nodes.Add(, , key_root, RsHLList(0), 1)
                Set node_2nd = TreeView1.Nodes.Add(key_root, tvwChild, key_2nd, RsHLList(1), 1)
                Set node_3rd = TreeView1.Nodes.Add(key_2nd, tvwChild, key_3rd & cnt, RsHLList(2), 1)
                
                tmpidx = RsHLList(1)
            End If
            
            .MoveNext
        Loop
        .Close
    End With
    
    For i = 1 To TreeView1.Nodes.Count
        TreeView1.Nodes(i).Expanded = True
    Next
    

Open in new window

sorry my mistake..
it was image list initializing problem ...
let me check again
it worked for me.

Private Sub test2()
    Dim Con_Main As ADODB.Connection
    Dim connstr As String
    
    
    ImageList1.ListImages.Add 1, "key1", LoadPicture("C:\test\tmp1.jpg")
    ImageList1.ListImages.Add 2, "key2", LoadPicture("C:\test\tmp2.jpg")
    ImageList1.ListImages.Add 3, "key3", LoadPicture("C:\test\tmp3.jpg")
    Set Me.TreeView1.ImageList = Me.ImageList1
    
    connstr = "Provider=MSOLEDBSQL;DataTypeCompatibility=80;Server=ServerName;Database=EE;UID=UserID;PWD=Password;"
    
    Set Con_Main = New ADODB.Connection
    Con_Main.ConnectionString = connstr
    Con_Main.Open
    
    Dim RsHLList As New ADODB.Recordset

    Dim cnt As Integer, tmpidx As Integer, i As Integer
    
    Dim node_root As Node
    Dim node_2nd As Node
    Dim node_3rd As Node
    Dim key_root As String
    Dim key_2nd As String
    Dim key_3rd As String

    Set RsHLList = Nothing
    If RsHLList.State = 1 Then RsHLList.Close
        
    RsHLList.Open "SELECT Distinct(EmpName), LoanAMT, RCVD_Amount  FROM HandLoan_DTL where Loan_ON_OFF = 'ON' order by EmpName", Con_Main, adOpenKeyset, adLockOptimistic
    
    
    TreeView1.Style = tvwTreelinesPlusMinusText
    
    With RsHLList
        Do While Not .EOF
            cnt = cnt + 1
            
            If tmpidx = RsHLList(1) Then
                Set node_3rd = TreeView1.Nodes.Add(key_2nd, tvwChild, key_3rd & cnt, RsHLList(2), 1)
            Else
                key_root = "root" & cnt
                key_2nd = "node_2nd" & cnt
                key_3rd = "node_3rd" & cnt
                
                Set node_root = TreeView1.Nodes.Add(, , key_root, RsHLList(0), 1)
                Set node_2nd = TreeView1.Nodes.Add(key_root, tvwChild, key_2nd, RsHLList(1), 1)
                Set node_3rd = TreeView1.Nodes.Add(key_2nd, tvwChild, key_3rd & cnt, RsHLList(2), 1)
                
                tmpidx = RsHLList(1)
            End If
            
            .MoveNext
        Loop
        .Close
    End With
    
    For i = 1 To TreeView1.Nodes.Count
        TreeView1.Nodes(i).Expanded = True
    Next
    
    Set RsHLList = Nothing
    Con_Main.Close
    Set Con_Main = Nothing
End Sub

Open in new window

Thanks Ryan Chong