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.
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.
And here’s the sample data content for this table which we are going to display it in our listview control later on.
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:
- 'this will serve as an open connection to our data source.
- Public con As OleDb.OleDbConnection = jokendb()
- 'this is some set of commands and a database connection that are use to fill later our datatable
- Dim da As New OleDb.OleDbDataAdapter
- 'this will represent as our table in memory
- Dim dt As New DataTable
- 'this holds our SQL Statements
- Dim sql As String
- Public Function jokendb() As OleDb.OleDbConnection
- Return New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\studinfo.accdb")
- End Function
- sql = "SELECT ID,firstname,lastname,gender,course FROM students"
- da = New OleDb.OleDbDataAdapter(sql, con)
- 'fill the data to datatable
- 'declare a variable as DataRow which is represent a row of data
- Dim newrow As DataRow
- 'loop from each row based on the number of rows stored in dt
- For Each newrow In dt.Rows
- 'display the results in listview
- ListView1.Items.Add(newrow.Item(0)) 'this is for ID
- ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(newrow.Item(1)) 'this is for Name
- ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(newrow.Item(2)) 'this is for Lastname
- ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(newrow.Item(3)) 'this is for gender
- ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(newrow.Item(4)) ' this is for course
- Catch ex As Exception
- End Try
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.