5 Replies - 5225 Views - Last Post: 22 March 2013 - 07:22 PM Rate Topic: -----

#1 jmProg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 04-July 12

How to place data from access into listview in VB

Posted 16 March 2013 - 06:58 AM

Hello. I need help. I need to show all the data into a listview coming from ms access. I already have this code:

   
 Dim SQL As String
    Dim con As New OleDbConnection
    Dim oData As OleDbDataAdapter
    Dim DS As New DataSet
    Dim dt As DataTable
    Dim com As OleDbCommand
    
con.ConnectionString = “”

con.Open()

        SQL = "SELECT * FROM Customer"
        com = New OleDbCommand(SQL, con)
        oData = New OleDbDataAdapter(com)
        DS = New DataSet
        oData.Fill(DS)

        lvCustomers.Clear()

        For i = 0 To DS.Tables(0).Rows.Count - 1
            lvCustomers.Items.Add(DS.Tables(0).Rows(i)("CustomerID").ToString)
            lvCustomers.Items.Add(DS.Tables(0).Rows(i)("FirstName").ToString)
            lvCustomers.Items.Add(DS.Tables(0).Rows(i)("LastName").ToString)
            lvCustomers.Items.Add(DS.Tables(0).Rows(i)("Address").ToString)
            lvCustomers.Items.Add(DS.Tables(0).Rows(i)("ContactNumber").ToString)
        Next
        
        con.Close()
	
	With lvCustomers
            .View = View.Details
            .GridLines = True
            .FullRowSelect = True

            lvCustomers.Columns.Add("Customer ID", 100, HorizontalAlignment.Center)
            lvCustomers.Columns.Add("First Name", 150, HorizontalAlignment.Center)
            lvCustomers.Columns.Add("Last Name", 150, HorizontalAlignment.Center)
            lvCustomers.Columns.Add("Address", 150, HorizontalAlignment.Center)
            lvCustomers.Columns.Add("Contact Number", 150, HorizontalAlignment.Center)
        End With




But then, the data just shows in the “CustomerID” Column .. How can I distribute other data to their respective columns? I think I need to do it with SubItems. But I really don’t know how to use it. Please help me. And may I suggest that you use easy codes (one that I can understand easily). I’m just a beginner. Sorry. And thanks for those who’ll help.. 

Is This A Good Question/Topic? 0
  • +

Replies To: How to place data from access into listview in VB

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: How to place data from access into listview in VB

Posted 16 March 2013 - 07:01 AM

If you know that you programming in VB.net, why you posting in the C# section?
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: How to place data from access into listview in VB

Posted 16 March 2013 - 07:30 AM

Moved to VB.NET
Was This Post Helpful? 0
  • +
  • -

#4 IronRazer  Icon User is online

  • D.I.C Addict

Reputation: 215
  • View blog
  • Posts: 727
  • Joined: 01-February 13

Re: How to place data from access into listview in VB

Posted 16 March 2013 - 07:51 AM

Here is a simple strait forward way to do it. You should be able to catch on to it. You will need to have 5 columns already added to the listview in its Column Collection properties and have the (View) property of the listview set to (Details)
        For i = 1 To DS.Tables(0).Rows.Count - 1
            Dim str1 As String = DS.Tables(0).Rows(i)("CustomerID").ToString
            Dim str2 As String = DS.Tables(0).Rows(i)("FirstName").ToString
            Dim str3 As String = DS.Tables(0).Rows(i)("LastName").ToString
            Dim str4 As String = DS.Tables(0).Rows(i)("Address").ToString
            Dim str5 As String = DS.Tables(0).Rows(i)("ContactNumber").ToString

            Dim lvi As New ListViewItem 'create new listview item
            lvi.Text = str1 'Put text in column 1
            lvi.SubItems.AddRange(New String() {str2, str3, str4, str5}) 'Put text in columns 2,3,4,5
            lvCustomers.Items.Add(lvi) 'Add the new row to listview
        Next


:bigsmile:/>
Edit : Forgot to change some lines.

This post has been edited by IronRazer: 16 March 2013 - 07:56 AM

Was This Post Helpful? 1
  • +
  • -

#5 jmProg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 04-July 12

Re: How to place data from access into listview in VB

Posted 22 March 2013 - 03:34 AM

Thanks for that IronRazer. My code is now working.. That made me understand more..
Sorry, I posted it first in C#, I didn't notice it.. Anyway, thanks JackOfAllTrades! :D

Btw, now I'm having problem with my listview.. How can I clear the items first loaded in my listview after modifying some data? I tried to use ListView1.Clear() but doesn't work so as the ListView1.Items.Clear() . any ideas about it? Thank you so much! :)
Was This Post Helpful? 0
  • +
  • -

#6 IronRazer  Icon User is online

  • D.I.C Addict

Reputation: 215
  • View blog
  • Posts: 727
  • Joined: 01-February 13

Re: How to place data from access into listview in VB

Posted 22 March 2013 - 07:22 PM

Hey jmProg,
If you are trying to clear all the items in the listview you should be able to use
        lvCustomers.Items.Clear()

:rolleyes2:/>

This post has been edited by IronRazer: 22 March 2013 - 07:29 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1