Load data into listview from Mysql Database using C#

In this article, we will see how to load data into listview from MySql Database using C sharp Windows Application. 

Install the Xampp or Wamp server in your computer or laptop and run it. Now open Visual Studio and create a new project.

Add Mysql.data.dll 

using System;

using Syst.emCollections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace ListViewTutorial
{
    public partial class frmListview : Form
    {
        public frmListview()
        {
            InitializeComponent();
        }
        private voidbutton5_Click(object sender, EventArgs e)
        {
             string con1 = “Data Source=localhost;Initial Catalog=test;uid=root”;
                       
            MySqlConnection  cnn = new MySqlConnection(con1);
            cnn.Open();
            string sql = “Select * from student”;
            MySqlCommand   cmd = new MySqlCommand(sql, cnn);
            MySqlDataReader  Reader = cmd.ExecuteReader();
            listView1.Items.Clear();
            while (Reader.Read())
            {
                ListViewItem lv = new ListViewItem(Reader.GetInt32(0).ToString());
                lv.SubItems.Add(Reader.GetString(1));
                lv.SubItems.Add(Reader.GetString(2));
                listView1.Items.Add(lv);
            }
            Reader.Close();
            cmd.Dispose();
            cnn.Close();
        }
        private voidfrmListview_Load(object sender, EventArgs e)
        {
            listView1.GridLines = true;
            listView1.View = View.Details;
            //Add Column Header
            listView1.Columns.Add(“Employee ID”, 150);
            listView1.Columns.Add(“First Name”, 150);
            listView1.Columns.Add(“Last Name”, 150);
        }
    }
}


One Comment on “Load data into listview from Mysql Database using C#”

Leave a Reply

Your email address will not be published. Required fields are marked *