Wrox Programmer Forums
|
VB How-To Ask your "How do I do this with VB?" questions in this forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 12th, 2004, 02:50 PM
Registered User
 
Join Date: Mar 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Add Data to Listview

I don't know much about the listview control. I am trying to figure out how to populate the listview with data from a table? I want to add ID, Date, and Desc. The user would then be able to pick what record they want to view.

How would I do this with the following data for example....

ID Date Desc
1 3/31/04 Test1
2 3/29/03 Test2
 
Old April 12th, 2004, 05:52 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 627
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You can find documentation in the msdn. Select the ListItems property of the ListView control and that gives you this example:

Private Sub Form_Load()
   ' Add ColumnHeaders. The width of the columns is
   ' the width of the control divided by the number of
   ' ColumnHeader objects.
   ListView1.ColumnHeaders. _
   Add , , "Author", ListView1.Width / 3
   ListView1.ColumnHeaders. _
   Add , , "Author ID", ListView1.Width / 3, _
   lvwColumnCenter
   ListView1.ColumnHeaders. _
   Add , , "Birthdate", ListView1.Width / 3
   ' Set View property to Report.
   ListView1.View = lvwReport

   ' Declare object variables for the
   ' Data Access objects.
   Dim myDb As Database, myRs As Recordset
   ' Set the Database to the BIBLIO.MDB database.
   ' IMPORTANT: the Biblio.mdb must be on your
   ' machine, and you must set the correct path to
   ' the file in the OpenDatabase function below.
   Set myDb = DBEngine.Workspaces(0) _
      .OpenDatabase("c:\Program Files\VB\BIBLIO.MDB")
   ' Set the recordset to the "Authors" table.
   Set myRs = _
   myDb.OpenRecordset("Authors", dbOpenDynaset)

   ' Declare a variable to add ListItem objects.
   Dim itmX As ListItem

   ' While the record is not the last record,
   ' add a ListItem object. Use the author field for
   ' the ListItem object's text. Use the AuthorID
   ' field for the ListItem object's SubItem(1).
   ' Use the "Year of Birth" field for the ListItem
   ' object's SubItem(2).

   While Not myRs.EOF
      Set itmX = ListView1.ListItems. _
      Add(, , CStr(myRs!Author)) ' Author.

      ' If the AuthorID field is not null, then set
      ' SubItem 1 to it.
      If Not IsNull(myRs!Au_id) Then
         itmX.SubItems(1) = CStr(myRs!Au_id)
      End If

      ' If the birth field is not Null, set
      ' SubItem 2 to it.
      If Not IsNull(myRs![Year Born]) Then
         itmX.SubItems(2) = myRs![Year Born]
      End If
      myRs.MoveNext ' Move to next record.
   Wend
 
Old April 13th, 2004, 07:55 AM
Registered User
 
Join Date: Mar 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Save Listview Data to a DataBase ecabralrojas Beginning VB 6 0 June 30th, 2008 10:33 AM
problem writing listview data to mysql db method Pro VB 6 2 April 10th, 2007 07:17 PM
problem writing listview data to mysql db method Pro VB Databases 0 May 17th, 2006 06:18 AM
Exporting listview data to excel file dotnetprogrammer VS.NET 2002/2003 2 May 10th, 2005 08:13 AM
Use a combo box to edit data in a listview goody_tim VB How-To 0 July 2nd, 2003 06:23 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.