Kamil Zahid

Kamil Zahid

  • NA
  • 6
  • 22.5k

How to Display Data from MySql Database on DataGridView in C#

Mar 27 2011 10:24 AM
Hi Guys

I really need some help here, I have used the following to code to display information from my database into visual Studio DataGridView but it displays the attributes and not the actual data values. I don't understand why:

        public void BindCustomerDataToGrid()
        {

           
            MySqlConnection cn = new MySqlConnection("SERVER=localhost;" + "DATABASE=clientmanagement;" + "UID=root;" + "PASSWORD=qwerty;");
            {
                DataTable customerTable = new DataTable();
                string query = "select c.customer_name, c.phone, a.ytd_sale from customer c, account a where c.customer_id = a.customer_id;";
                MySqlDataAdapter dataAdaptar = new MySqlDataAdapter(query, cn);
                MySqlCommandBuilder cmd = new MySqlCommandBuilder(dataAdaptar);

              
                customerTable.Locale = System.Globalization.CultureInfo.InvariantCulture;
                dataAdaptar.Fill(customerTable);
                BindingSource customerDataBindingSource = new BindingSource();

                customerDataBindingSource.DataSource = customerTable;
                dataGridView1.DataSource = customerDataBindingSource;             
              
              
            }
        }

Your Help will be really much appreciated.


Answers (1)