Click here to Skip to main content
14,326,294 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi! All
I am student of C# beginner. I want to load mysql table to datagridview of C#. Please someone help me ! Thanks you all.
Posted
Updated 19-Oct-12 6:39am
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

1) Create a connection to your database.
2) Query for the data you want to show.
3) Bind that data to a datagridview control that you have placed on your form.
4) When something code related in this process doesn't work, ask that specific question here so we can help you.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

   
Rate this:
Please Sign up or sign in to vote.

Solution 3

Hi
To communicate with mysql you need to add mysql.data.dll in your project
You can download this dll from Here

after that add the namespace
using MySql.Data.MySqlClient;

MySqlConnection conn = new MySqlConnection(connectionstring);
MySqlCommand cmd = new MySqlCommand("Your sql query");
MySqlDataReader dr=cmd.ExecuteReader();

gridview1.datasource=dr;
gridview1.databind();


your connection string will be like this
<connectionstrings>
		<add name="connectionstring" connectionstring="Data source=server;database=mydb;user id=root;password=1234" providername="MySql.Data.MySqlClient" />
	</connectionstrings>


If you need documentation Click Here

I hope this will help you....
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 5

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Threading;

namespace FINALIST
{
public partial class Purchase : Form
{
public Purchase()
{
InitializeComponent();
load_table();
}


void load_table()
{
string constring = "datasource=localhost;port=3306;username=root;password=triple333";
MySqlConnection conDataBase = new MySqlConnection(constring);

MySqlCommand cmdDataBase = new MySqlCommand(" select date as 'Date',voucherNo as 'Voucher No',vendorName as 'Vendor Name',product as 'Product' from test.purchase ;", conDataBase);

//date, voucherNo, vendorName and product are variables of purchase table of test database.
try
{

MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmdDataBase;
dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bSource = new BindingSource();

bSource.DataSource = dbdataset;
dataGridView1.DataSource = bSource;
sda.Update(dbdataset);

}
catch (Exception ex)
{

MessageBox.Show(ex.Message);
}

}


}
}
   
Rate this:
Please Sign up or sign in to vote.

Solution 6

Note-if you want more about c# and mysql go to youtube.com and search for "programming knowledge" channel.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100