0

I have 4 textboxes and a datagridview, I wrote this code to display the selected row in a texboxes

private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
       {
           textBox1.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
           textBox2.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
           textBox3.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
           textBox4.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
       }

and I wrote this code to update the selected row

private void button5_Click_1(object sender, EventArgs e)
       {
                 SqlConnection con = new SqlConnection();
                conn.ConnectionString = (@"Data Source=Ali-pc\sqlexpress;Initial Catalog=StockDB;Integrated Security=True");
                conn.Open();
                SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM Stock", conn);
               DataTable dt = new DataTable();
               sda.Fill(dt);
               int selectedRow;
               selectedRow = dataGridView1.SelectedRows[0].Index;
               dataGridView1.Rows[selectedRow].SetValues(textBox1.Text, textBox2.Text, textBox3.Text, textBox3.Text);

               MessageBox.Show(" Successfully Saved! ");
}

And the codes work correctly, but my question is how can I save the changes to database ??

  • 2
    Possible duplicate of [inserting data from datagridview to database](http://stackoverflow.com/questions/11927864/inserting-data-from-datagridview-to-database) – Ghasem Nov 21 '15 at 08:00
  • Are you looking for [`SqlDataAdapter.Update()`](https://msdn.microsoft.com/en-us/library/33y2221y%28v=vs.110%29.aspx)? See maybe [SqlDataAdapter.update() not updating database](http://stackoverflow.com/questions/29483704) or [Comparison of dataAdapter .Fill and .Update](http://stackoverflow.com/questions/14305447). – dbc Nov 21 '15 at 08:54

0 Answers0