使用datagridview C#更新数据库

时间:2015-11-21 07:54:39

标签: c# datagridview

我有4个textboxesdatagridview,我编写了此代码以在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 ??

0 个答案:

没有答案