2

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 3

9

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];
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

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}"
0

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

http://www.youtube.com/watch?v=D0bRgwuUNUg

http://www.youtube.com/watch?v=Sm5mxkytfWk

Comments

0
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.