Load Datagridview from a Mysql database in C#


Download 31.39 Kb.
Sana14.03.2023
Hajmi31.39 Kb.
#1266894
Bog'liq
8Load Datagridview from a MySQL Database in C#


Load Datagridview from a MySQL Database in C#
In this tutorial, I will show you how to load MySQL Database Value in DataGridView using C# in a simple way.
Let’s follow tutorial below.
Step 1:
Create Project And Add Reference To C# WinForms Project. (Please link these to my first article connect mysql to c#)
Step 2:
Open MySQL Workbench, right click and create schema (new database), give database name as “sampledb” and create table in database and give a name as “information”, then create columns id, emp_id, name, designation, username and password and then insert data into table (insert-save data to MySQL Database using c#) please link these to my third article

Step 3:
Back to the windows forms application and design forms like this

  • txtemp_id

  • txtname

  • txtdesignation

  • txtusername

  • txtpassword

  • btnadd

  • btnedit

  • btndelete

  • DataGridView1

Step 4:


using System;
using System.Collections.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 CSHARP_FULL_COURSE_WITH_MYSQL_DATABASE
{
public partial class information : Form
{
public information ()
{
InitializeComponent();
}
private void fillcombobox_Load(object sender, EventArgs e)
{
MySqlConnection con = new MySqlConnection("datasource= localhost; database=sampledb;port=3306; username = root; password= db1234"); //open connection
con.Open();
DataGridView1.DataSource = null;
MySqlDataAdapter adapter = new MySqlDataAdapter("select * from information",con);
DataTable dt = new DataTable();
adapter.Fill(dt);
DataGridView1.DataSource = dt;
con.Close();
}


}
}


Source code for Forms Load events:

Code Explanation:


This code is for loading or viewing data from a database into DataGridView.
Result:

Mark Jaylo


markjaylo26@gmail.com
https://www.youtube.com/watch?v=Lm5f9x7aGko
Download 31.39 Kb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling