i have a table in mysql database named cms.order and i am working on a widows form using C#. How can i show the data from the table in database in the datagridview of the windows form
3 Answers
You can use the following code to read the table data from database and display it into data gridview.
string connectionString = ""; //Set your MySQL connection string here.
string query =""; // set query to fetch data "Select * from tabelname";
using(MySqlConnection conn = new MySqlConnection(connStr))
{
using(MySqlDataAdapter adapter = new MySqlDataAdapter(query, conn))
{
DataSet ds = new DataSet();
adapter.Fill(ds);
DataGridView1.DataSource= ds.Tables[0];
}
}
1 Comment
ayla
just in case anyone is using wpf instead of winforms, do the following: datagrid1.DataContext = ds.Tables[0].DefaultView; - and then in the xaml for the datagrid, add ItemsSource="{Binding}"
With a simple search on the internet you can find a simple solution...
http://www.dotnetperls.com/datagridview-tutorial
http://www.dotnetperls.com/datatable
From youtube, you can also get this information
http://www.youtube.com/watch?v=peiorDq5oF0
Comments
MySqlConnection con= new MySqlConnection("server=localhost;database=databasename;user=username;password=password");
string query="select *from table";
using (MySqlDataAdapter adpt= new MySqlDataAdapter(query,con))
{
DataSet dset= new DataSet();
adpt.Fill(dset);
mytableDataGridView.DataSource=dset.Tables[0];
}
con.close