Link to home
Start Free TrialLog in
Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Bind data to ListView in VB.net

Hi

I am tring to bind the contents of my Access table "Rooms" using the following code
but am getting an error that datasource is not a member of the ListView
Sub Fill_Rooms_ListView()
        Try
            Dim sSQL As String

            sSQL = "SELECT * FROM [Rooms]"
            Dim connection As New OleDbConnection(ConnectionString)
            dbadp = New OleDbDataAdapter(sSQL, connection)

            dbadp.Fill(dTable)
            Me.ListView_Rooms.DataSource = dTable


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Carlos Villegas
Carlos Villegas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Murray Brown

ASKER

thanks very much
I recommend you this code instead the first one:
Sub Fill_Rooms_ListView()
    Try
        Me.ListView_Rooms.Items.Clear()
        Me.ListView_Rooms.BeginUpdate()
        Using myConnection As New OleDbConnection(ConnectionString)
            Dim myCommand As New OleDbCommand("SELECT * FROM [Rooms]", myConnection)
            myConnection.Open()
            Using dr As OleDbDataReader = myCommand.ExecuteReader
                Do While dr.Read
                    Dim myItem As ListViewItem = Me.ListView_Rooms.Items.Add(dr("MyColumnA").ToString())
                    myItem.SubItems.Add(dr("MyColumnB").ToString())
                    myItem.SubItems.Add(dr("MyColumnC").ToString())
                Loop
            End Using
        End Using
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        Me.ListView_Rooms.EndUpdate()
    End Try
End Sub

Open in new window

Also, first you must create the columns of your ListView, this can be done by using the designer.
Thanks
Glad to help