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 :(

Recommended Answers

All 2 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.