1
Answer

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

Kamil Zahid

Kamil Zahid

13y
13.7k
1
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)
Next Recommended Forum