我有4个textboxes
和datagridview
,我编写了此代码以在row
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();
}
我写了这段代码来更新选定的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! ");
}
代码工作正常,但我的问题是如何将更改保存到database
??