Load data to ListView C#

Load data to ListView C#
Load data to ListView C#
Load data to ListView C#
In this article I will explain with an example, how to populate (bind) ListView from Database in Windows Forms Application (WinForms) using C# and VB.Net.
This article will illustrate how to populate data from SQL Server Database in DataTable and then use DataTable to populate ListView control in Windows Forms (WinForms) Application using C# and VB.Net.
Database
I have made use of the following table Customers with the schema as follows.
Load data to ListView C#
I have already inserted few records in the table.
Load data to ListView C#
Note: You can download the database table SQL by clicking the download link below.
Download SQL file

Form Design
The Form consists of a ListView control.
Load data to ListView C#
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Populating ListView from Database using C# and VB.Net
Inside the Form Load event, the records from the database are fetched into a DataTable.
Then a loop is executed over the DataTable rows and one by one Item is added to the ListView.
C#
private void Form1_Load(object sender, EventArgs e)
{
string constr = @"Data Source=.\SQL2017;Initial Catalog=AjaxSamples;Integrated Security=true";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlDataAdapter sda = new SqlDataAdapter("SELECT CustomerId, Name FROM Customers", con))
{
//Fill the DataTable with records from Table.
DataTable dt = new DataTable();
sda.Fill(dt);
//Loop through the DataTable.
foreach (DataRow row in dt.Rows)
{
//Add Item to ListView.
ListViewItem item = new ListViewItem(row["Name"].ToString());
item.SubItems.Add(row["CustomerId"].ToString());
lvCustomers.Items.Add(item);
}
lvCustomers.View = View.List;
}
}
}
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim constr As String = "Data Source=.\SQL2017;Initial Catalog=AjaxSamples;Integrated Security=true"
Using con As SqlConnection = New SqlConnection(constr)
Using sda As SqlDataAdapter = New SqlDataAdapter("SELECT CustomerId, Name FROM Customers", con)
'Fill the DataTable with records from Table.
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
'Loop through the DataTable.
For Each row As DataRow In dt.Rows
'Add Item to ListView.
Dim item As ListViewItem = New ListViewItem(row("Name").ToString())
item.SubItems.Add(row("CustomerId").ToString())
lvCustomers.Items.Add(item)
Next
lvCustomers.View = View.List
End Using
End Using
End Sub
Implementing SelectedIndexChanged event handler of the ListView
The ListView has been attached a SelectedIndexChanged event handler.
Inside the SelectedIndexChanged event handler, the Text parts of the Selected Item and SubItem of the ListView is displayed using MessageBox.
C#
private void lvCustomers_SelectedIndexChanged(object sender, EventArgs e)
{
if (lvCustomers.SelectedItems.Count > 0)
{
string message = "Name: " + lvCustomers.SelectedItems[0].Text + Environment.NewLine;
message += "ID: " + lvCustomers.SelectedItems[0].SubItems[1].Text;
MessageBox.Show(message);
}
}
VB.Net
Private Sub ListView1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lvCustomers.SelectedIndexChanged
If lvCustomers.SelectedItems.Count > 0 Then
Dim message As String = "Name: " & lvCustomers.SelectedItems(0).Text & Environment.NewLine
message &= "ID: " & lvCustomers.SelectedItems(0).SubItems(1).Text
MessageBox.Show(message)
End If
End Sub
Screenshot
Load data to ListView C#
Downloads
ListView_Windows.zip
Download Free Word/PDF/Excel API