We help IT Professionals succeed at work.

How to create tree view with sub view

Altaf Khan
Altaf Khan asked
on
72 Views
Last Modified: 2020-04-19
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
Comment
Watch Question

CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Altaf KhanStudent

Author

Commented:
thanks for reply..
let me try
Altaf KhanStudent

Author

Commented:
Getting run time error 13 - type mismatch

and highlighted on
               Set node_root = TreeView1.Nodes.Add(, , key_root, RsHLList(0), 1)
CERTIFIED EXPERT

Commented:
Getting run time error 13 - type mismatch

have you declared the object properly?

Dim node_root As Node

Open in new window

Altaf KhanStudent

Author

Commented:
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

Altaf KhanStudent

Author

Commented:
sorry my mistake..
it was image list initializing problem ...
let me check again
CERTIFIED EXPERT

Commented:
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

Altaf KhanStudent

Author

Commented:
Thanks Ryan Chong

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.