How to Load Data in the Listview Using C# and MySQL Database

The Listview control is used to display a collection of items. This also provides many ways to arrange and display items with item text and an icon (optionally) is used to determine the type of item. So, now I’m going to teach you how to load data in the ListView using C# and MySQL Database. This is just a simple program but I’m pretty sure that this method will help you when you encounter problem of displaying set of data in the database and can be displayed in the Listview. So let’s get started.

Creating Database

Create a database and named it “salesdb”; Execute the following query to add the table with data in the database.
  1. CREATE TABLE `tblsales` (
  2.   `SalesId` int(11) NOT NULL,
  3.   `TRANSDATE` date NOT NULL,
  4.   `Product` varchar(90) NOT NULL,
  5.   `TotalSales` double NOT NULL
  6.  
  7. --
  8. -- Dumping data for table `tblsales`
  9. --
  10.  
  11. INSERT INTO `tblsales` (`SalesId`, `TRANSDATE`, `Product`, `TotalSales`) VALUES
  12. (1, '2018-01-30', 'Cellphone', 1400),
  13. (2, '2018-02-28', 'Laptop', 800),
  14. (3, '2018-03-31', 'Desktop', 5052),
  15. (4, '2019-04-30', 'Ipod', 8030),
  16. (5, '2019-05-31', 'Tablet', 10000);

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application. createapp

Step 2

Add a ListView inside the Form and do the Form just like this. formListView

Step 3

Open the code editor and add a namespace to access MySQL Library.
  1. using MySql.Data.MySqlClient;

Step 4

Create a method for retrieving data in the database to be displayed in the ListView.
  1. private void LoadData()
  2.         {
  3.             MySqlConnection con = new MySqlConnection("server=localhost;user id=root;password=;database=salesdb;sslMode=none");
  4.             MySqlCommand cmd;
  5.             MySqlDataAdapter da;
  6.             DataTable dt;
  7.              
  8.  
  9.             try
  10.             {
  11.                 con.Open();
  12.                 cmd = new MySqlCommand();
  13.                 cmd.Connection = con;
  14.                 cmd.CommandText = "Select * FROM tblsales";
  15.                 da = new MySqlDataAdapter();
  16.                 da.SelectCommand = cmd;
  17.                 dt = new DataTable();
  18.                 da.Fill(dt);
  19.  
  20.                 for (int i = 0; i < dt.Rows.Count; i++)
  21.                 {
  22.                     DataRow dr = dt.Rows[i];
  23.                     ListViewItem listitem = new ListViewItem(dr["SalesId"].ToString());
  24.                     listitem.SubItems.Add(dr["TRANSDATE"].ToString());
  25.                     listitem.SubItems.Add(dr["Product"].ToString());
  26.                     listitem.SubItems.Add(dr["TotalSales"].ToString());
  27.                     listView1.Items.Add(listitem);
  28.                 }
  29.  
  30.             }
  31.             catch(Exception ex)
  32.             {
  33.                 MessageBox.Show(ex.Message);
  34.             }
  35.             finally
  36.             {
  37.                 con.Close();
  38.             }
  39.         }

Step 5

Call the method that you have created and put it inside the Load event handler to perform in the first load of the Form
  1. private void Form1_Load(object sender, EventArgs e)
  2. {
  3.     LoadData();
  4. }

Add new comment