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
??