How can i update data from data grid view into SQL database using C#. Actually i m working on windows form app and i have to write update code in click event of the button.I am trying to update single row of data grid view into database. I have tried many sql queries for it but its not working :(
please help me :(

Edited by SHaseeb: i forgot

5 Years
Discussion Span
Last Post by SHaseeb

When populating DGV, do it with databinding from DataTable. So when changes to DGV will be made, all will reflect in datatable as well. Then do an Update sql query to update database.
Check here how it can be done.


I have tried this code under a button like this

private void button9_Click(object sender, EventArgs e)

      DataSet ds;
      ds = new DataSet();
  // 1. Create connection.
      MySqlCommand cmd = con.CreateCommand();
  // 2. init SqlDataAdapter with select command and connection
  MySqlDataAdapter da = new MySqlDataAdapter("Select * from model", con);
  // 3. fill in insert, update, and delete commands
  MySqlCommandBuilder cmdBldr = new MySqlCommandBuilder(da);
  da.Fill(ds, "model");
  dataGridView2.DataSource = ds;
  //dataGridView2.DataMember = "Model";
    da.Update(ds, "model");

*but when i change a record in the datagrid and press this button the datagrid becomes empty and nothing is updated to the database. please check it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.