使用datagridview C#更新数据库 [英] Update Database using datagridview C#

查看:81
本文介绍了使用datagridview C#更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有4个文本框和一个datagridview,我写了这段代码来显示texbox中的选定行





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! ");







代码工作正常,但我的问题是如何保存对数据库的更改??

推荐答案

在网上查看SqlDataAdaptor更新方法



iirc,鉴于你的代码,它看起来像



Have a look on the net for SqlDataAdaptor Update method

iirc, given your code, it would look like

sda.Update(dt);





但我不确定你还需要做什么,即前提条件



but Im not sure what else you would need to do ie pre-conditions


这篇关于使用datagridview C#更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆