How to Populate Listview with Data from Database using Visual Basic.Net

In Windows from application, one of the most choice of a programmer used to display vast amount of data on the screen aside from datagridview is Listview control.Because Listview control provides the infrastructure to display a set of data items in different layouts or views. In this tutorial, i'm going to show you how to populate listview control with data from Microsoft Access database using Visual Basic.Net. to start with let’s a new project in Visual Basic called “Listview”. Then we will add a listview control from toolbox. Next, add a column Header that looks like as shown below. h1 At this, lets’ create a new Database in Microsoft Access 2007 and we will save it as “studinfo”. Then create a table named “students”. And here’s the design for this table. h2 And here’s the sample data content for this table which we are going to display it in our listview control later on. h3 At this time, lets save this database inside the debug folder of our project. Then we will go back to our application. to start adding functionality to our application, double click the form, and add the following declaration outside any “private sub”. And here’s the following code:
  1. 'this will serve as an open connection to our data source.
  2. Public con As OleDb.OleDbConnection = jokendb()
  3. 'this is some set of commands and a database connection that are use to fill later our datatable
  4. Dim da As New OleDb.OleDbDataAdapter
  5. 'this will represent as our table in memory
  6. Dim dt As New DataTable
  7. 'this holds our SQL Statements
  8. Dim sql As String
As we observe to code above, we have assigned values to variable “con” from a function called “jokendb()” which is returing new set of values containing the provider and the data source of our database. and here’s the following code for this function:
  1. Public Function jokendb() As OleDb.OleDbConnection
  2. Return New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\studinfo.accdb")
  3. End Function
And finally, when the form loads at start up. It will loads all the data from the database to listview control. And here’s the following code:
  1. Try
  2. sql = "SELECT ID,firstname,lastname,gender,course FROM students"
  3. con.Open()
  4. da = New OleDb.OleDbDataAdapter(sql, con)
  5. 'fill the data to datatable
  6. da.Fill(dt)
  7. 'declare a variable as DataRow which is represent a row of data
  8. Dim newrow As DataRow
  9. 'loop from each row based on the number of rows stored in dt
  10. For Each newrow In dt.Rows
  11. 'display the results in listview
  12. ListView1.Items.Add(newrow.Item(0)) 'this is for ID
  13. ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(newrow.Item(1)) 'this is for Name
  14. ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(newrow.Item(2)) 'this is for Lastname
  15. ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(newrow.Item(3)) 'this is for gender
  16. ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(newrow.Item(4)) ' this is for course
  17.  
  18.  
  19. Next
  20.  
  21. Catch ex As Exception
  22. MsgBox(ex.Message)
  23.  
  24. End Try
Then you can run the application and test it, it should look like as shown below: h4

Add new comment