Murray Brown
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
Also, first you must create the columns of your ListView, this can be done by using the designer.
ASKER
Thanks
Glad to help
ASKER