C# – Datagridview cell value change update database

cdatagridviewMySQL

I have retrieved data from Mysql database into a DataGridView1. Let us suppose I am in Row 0. When I change the contents of Row 0, Cell 1 and press enter key or a button, the Update query should modify that row, but I am unable to modify the value of the cell. The cell maintains its previous value when i reload data and the database is not modified. For example, if I change the contents of a cell under column Client_Name from "Acs" to "Gmt", how can I change the value of the cell from "Acs" to "Gmt"? and to have it updated into Mysql database, I am using c# in Vs 2012. below is my code that retrieves my database into datagridview1 any help is welcomed thanks.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.Data.SqlClient;

namespace PI.Gen
{
    public partial class frmMain : Form
    {
        MySqlConnection Conn;


        public frmMain()
        {
            InitializeComponent();
            btnDisconnect.Enabled = true;
            btnLoadData.Enabled = false;
            btnLoadClients.Enabled = false;

        }





        private void btnConnect_Click(object sender, EventArgs e)
        {

            string strConnect = "server=" + txtServer.Text + ";uid=" + txtUsername.Text + ";pwd=" + txtPassword.Text + ";database=" + txtDatabase.Text;
            try
            {
                if (txtServer.TextLength <= 0 || txtUsername.TextLength <= 0 || txtDatabase.TextLength <= 0)
                {
                    MessageBox.Show("You have an empty database connection field. Please supply a valid value.");
                    return;
                }

                Conn = new MySqlConnection(strConnect);
                Conn.Open();

                if (Conn.State.ToString() != "Open")
                {
                    MessageBox.Show("Could not open database connection");
                    return;
                }
                btnDisconnect.Enabled = true;
                btnConnect.Enabled = false;
                btnLoadData.Enabled = true;
                btnLoadClients.Enabled = true;
                //  btnSubmitClient.Enabled = true;
            }
            catch (Exception ex)  // catch on general exceptions, not specific
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }



        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (Conn != null)
            {
                Conn.Close();
            }
        }

        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            try
            {
                Conn.Close();
                Conn = null;
                btnDisconnect.Enabled = false;
                btnConnect.Enabled = true;
                btnLoadData.Enabled = false;
                btnLoadClients.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }





        private void btnLoadData_Click(object sender, EventArgs e)
        {
            try
            {
                string CmdString = "SELECT * FROM t_receipients";
                MySqlDataAdapter sda = new MySqlDataAdapter(CmdString, Conn);
                DataSet ds = new DataSet();

                sda.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0].DefaultView;


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

         }


        private void btnLoadClients_Click(object sender, EventArgs e)
        {
            try
            {

                string CmdString = "SELECT * FROM t_clients";
                MySqlDataAdapter sda = new MySqlDataAdapter(CmdString, Conn);

                DataSet ds = new DataSet();
                sda.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0].DefaultView;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

        }

Best Answer

After series of trials and error, i finally found what i was looking for, thus being able to update database from datagridview below is my worked around code which works 100% hope it helps someone in future, and thanks @RageComplex for helping out, but one more thing does anyone know how to implement that i mean instead of hitting the enter button to take changes in the datagridview you rather click on a button ty

   private void dataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e)
    {
        try
        {
            DataTable changes = ((DataTable)dataGridView1.DataSource).GetChanges();
            if (changes != null)
            {
                MySqlCommandBuilder mcb = new MySqlCommandBuilder(mySqlDataAdapter);
                mySqlDataAdapter.UpdateCommand = mcb.GetUpdateCommand();
                mySqlDataAdapter.Update(changes);
                ((DataTable)dataGridView1.DataSource).AcceptChanges();

                MessageBox.Show("Cell Updated");
                return;
            }


        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }


    }