?
Solved

Bind data to ListView in VB.net

Posted on 2011-10-15
5
Medium Priority
?
918 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:Murray Brown
  • 3
  • 2
5 Comments
 
LVL 17

Accepted Solution

by:
Carlos Villegas earned 2000 total points
ID: 36973569
Hello, that is because you are using code for a aspnet System.Web.UI.WebControls.ListView object, but what you really want is to bind data to a System.Windows.Forms.ListView control, this control dont support that kind of binding, instead you need to create each item programmatically, here an example:
Sub Fill_Rooms_ListView()
    Try
        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)
    End Try
End Sub

Open in new window

MyColumnA, MyColumnB and MyColumnC are examples, I dont know what columns has your Rooms table.
0
 

Author Closing Comment

by:Murray Brown
ID: 36973575
thanks very much
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 36973579
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.
0
 

Author Comment

by:Murray Brown
ID: 36973666
Thanks
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 36973670
Glad to help
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month17 days, 9 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question