473,787 Members | 2,932 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding Items to TreeView & ListView from Database.

debasisdas
8,127 Recognized Expert Expert
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
0 16299

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1672
by: Angelina | last post by:
Hi, I wanted to add a few items to my listview control. The values of these will be obtained from other controls on my interface. When the user clicks the select button, they will need to be added to my listview. This is what i need help doing. I have added the columns to the listview but i now want to add the values that correspond to these columns so that they all appear in a single row.
9
6129
by: Eva | last post by:
Hi, I wanted to know how i can enter values into a specific column of a listview. I have tried the following code but this seems to enter all my values into the first column!!! Can anyone please help me out on this?? hers my code so far.....
1
6062
by: Daisy | last post by:
I'm writing a NewsReader (seemed like a decent project to start learning with), and want threading like in OE. I've got a treeview and a listview. On Expand/Contract on the listview, I spin through the nodes checking .IsVisible, and those that are visible, I take the info I need (posted name / date) out of the .Tag (is there a better place to put this?), and add them to the listview. For now, it loops from the top, though it might be...
2
9042
by: cybertof | last post by:
Hello, Is there a solution to the following problem : When filling a listview (30 columns) with around 5000 items, it can take easily 10 sec for the listview to be filled. I have used BeginUpdate and SuspendLayout but no speed... Here is a flavor of the code :
3
2614
by: Holmes | last post by:
Hello Ran into a bit of a problem here and have now exhausted my resources to getting this working What I am trying to do is load and show a simple vb form with a listbox in it Dim frm_nc_code As New frm_nc_sen frm_nc_code.Show( Well what I want to have happen is it loads the form then shows all of the controls on the form (especially the listview control which should be blank at this time)
20
6678
by: Ash Phillips | last post by:
Hi Everyone, I have this program I wrote in VB6 for family use. It's a DVD Database just for me to keep track of them cause I have so many lol. In VB6, I could add items to the ListView in 'frmMain' from 'frmAdd' with the following code: Private Function AddEntry(Title As String, Rating As String, Genre As String, OnLoan As Boolean, ToWho As String)
0
1245
by: James L | last post by:
I have 2 different forms that contain treeviews and listviews. The former is used to populate the latter. For some reason the control randomly lose the images. So you cannot tell in the listview which items have been selected. I have recently tried assigning the images to the controls after initialize component and it is a little more stable, but if you close the form and immediately go back in the images are lost.
6
9975
by: Beginner | last post by:
Hi, I'm trying to populate a TreeView from an existing and filled in ListView (lvwBuild). lvwBuild is a basic two column listview which can have any number of rows. I would like the first column of lvwBuild to be a node and the second column to be a branch on the TreeView (hopefully I have the syntax correct). I've been researching how to do this but have come up empty. I'm guessing that you would fill an array from lvwBuild and...
3
2135
by: ChinChin | last post by:
Hi, How to make the nodes (Tree View control) referring to the files stored in my local directory (e.g.: drive C) and display the file details in List View control? Thanks.
0
9655
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9497
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.