By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,665 Members | 1,576 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

Adding Items to TreeView & ListView from Database.

debasisdas
Expert 5K+
P: 8,127
This sample code displays employee name in the treeview control from the emp table of Scott schema in oracle database.

To start with

Select Microsoft windows common controls 6.0 (SP6) from components
Add a TreeView and a ListView control to the form.

Add this sample code to the form
==========================
Expand|Select|Wrap|Line Numbers
  1. Dim con As New ADODB.Connection
  2. Dim rs As New ADODB.Recordset
  3. Dim rs1 As New ADODB.Recordset
  4.  
  5. Private Sub Form_Load()
  6. con.Open "Provider=OraOLEDB.Oracle.1;Password=tiger;Persist Security Info=True;User ID=scott;Data Source=das"
  7.  
  8. call TREE
  9. End Sub
  10.  
  11. Public Sub TREE()
  12. tv1.Visible = False
  13. tv1.Nodes.Clear
  14. tv1.Visible = True
  15.  Dim scontactname As String
  16.  Dim currentalpha As String
  17.  
  18.     rs1.Open "select * from emp  order by ename", con, adOpenDynamic, adLockOptimistic
  19.     If (rs1.RecordCount > 0) Then
  20.         rs1.MoveFirst
  21.     End If
  22.     For indx = Asc("A") To Asc("Z")
  23.         currentalpha = Chr(indx)
  24.         Set contactnode = tv1.Nodes.Add(, , currentalpha, currentalpha)
  25.     If (Not rs1.EOF) Then
  26.         Do While UCase(Left(rs1!ename, 1)) = currentalpha
  27.            With rs1
  28.                 If (Not IsNull(!ename)) Then
  29.                     scontactname = !ename
  30.                 End If
  31.             End With
  32.             DoEvents
  33.                 Set contactnode = tv1.Nodes.Add(currentalpha, tvwChild, rs1!ename, scontactname)
  34.                 rs1.MoveNext
  35.                 If (rs1.EOF) Then
  36.                     Exit Do
  37.                 End If
  38.                 Loop
  39.                 End If
  40.                 Next
  41.  
  42.                 rs1.Close
  43.                 DoEvents
  44. End Sub
  45.  
To display the details of the record as selected from the Treeview control.

Add the column headers in the listview from property pallet as the field name from the table if desired.

Add the following sample code
========================
Expand|Select|Wrap|Line Numbers
  1. Private Sub tv1_NodeClick(ByVal Node As MSComctlLib.Node)
  2. LV1.ListItems.Clear
  3. rs.Open "select * from EMP where ENAME = '" & tv1.SelectedItem.Key & "'", con, adOpenDynamic, adLockOptimistic
  4.  
  5. Dim li1 As ListItem
  6. Set li1 = LV1.ListItems.Add()
  7. If Not rs.EOF Then
  8.  
  9. If IsNull(rs(0)) Then
  10. li1.Text = ""
  11. Else
  12. li1.Text = rs(0)
  13. End If
  14.  
  15. If IsNull(rs(1)) Then
  16. LV1.ListItems(1).ListSubItems.Add , , ""
  17. Else
  18. LV1.ListItems(1).ListSubItems.Add , , rs(1)
  19. End If
  20.  
  21. If IsNull(rs(2)) Then
  22. LV1.ListItems(1).ListSubItems.Add , , ""
  23. Else
  24. LV1.ListItems(1).ListSubItems.Add , , rs(2)
  25. End If
  26.  
  27. If IsNull(rs(3)) Then
  28. LV1.ListItems(1).ListSubItems.Add , , ""
  29. Else
  30. LV1.ListItems(1).ListSubItems.Add , , rs(3)
  31. End If
  32. If IsNull(rs(4)) Then
  33. LV1.ListItems(1).ListSubItems.Add , , ""
  34. Else
  35. LV1.ListItems(1).ListSubItems.Add , , rs(4)
  36. End If
  37. If IsNull(rs(5)) Then
  38. LV1.ListItems(1).ListSubItems.Add , , ""
  39. Else
  40. LV1.ListItems(1).ListSubItems.Add , , rs(5)
  41. End If
  42. If IsNull(rs(6)) Then
  43. LV1.ListItems(1).ListSubItems.Add , , ""
  44. Else
  45. LV1.ListItems(1).ListSubItems.Add , , rs(6)
  46. End If
  47. If IsNull(rs(7)) Then
  48. LV1.ListItems(1).ListSubItems.Add , , ""
  49. Else
  50. LV1.ListItems(1).ListSubItems.Add , , rs(7)
  51. End If
  52.  
  53. End If
  54. rs.Close
  55. End Sub
  56.  
Oct 21 '07 #1
Share this Article
Share on Google+