0

Hi there i am trying to load MySql table data into a datagridview in my win forms application on button click, and cant seem to get it to work. I think im missing the binding source but im very confused.

Some help would be appreciated.

private void button3_Click(object sender, EventArgs e)
     {
         string connStr = "Data Source=localhost;port=3306;Initial Catalog=bitdb;User Id=root;Password='';"; 
         string query = "Select * from Client"; 
         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
  • What does the debugger say? Getting the table successful? Including some columns? Also, don't you need some column definitions on the datagridview? Commented May 31, 2017 at 7:48

1 Answer 1

3

According to the documentation you should use a BindingSource with the DataGridView component. More information here.

private void button3_Click(object sender, EventArgs e)
     {
         string connStr = "Data Source=localhost;port=3306;Initial Catalog=bitdb;User Id=root;Password='';"; 
         string query = "Select * from Client"; 
         using (MySqlConnection conn = new MySqlConnection(connStr))
         {
             using (MySqlDataAdapter adapter = new MySqlDataAdapter(query, conn))
             {
                 DataSet ds = new DataSet();
                 adapter.Fill(ds);
                 var bindingSource = new BindingSource();
                 bindingSource.DataSource = ds.Tables[0];
                 dataGridView1.DataSource = bindingSource;
             }
         }
     }

Also, any error information would be helpful.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks yer i was missing the binding the source iv got the data displaying now. Cheers again

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.