大家好!  我现在在dataGridView中修改、添加和删除数据,请问我要怎么做才能将更改的数据返回到数据库中?以下是部分代码。愿赐教!
              string connstring = @"server=localhost;integrated security=true;database=ps";
            SqlConnection conn = new SqlConnection(connstring);
            conn.Open();
            DataSet ds = new DataSet();
            try
            {
                string sql = @"select * from worker_table  where worker_id='" + this.textBox1.Text.Trim() + "'";
                SqlDataAdapter sdal = new SqlDataAdapter(sql, conn);
                sdal.Fill(ds, "tb1");                BindingSource bs = new BindingSource();
                bs.DataMember = "tb1";
                bs.DataSource = ds;
                this.dataGridView1.DataSource = bs;
            }

解决方案 »

  1.   

    定义:
    CellEndEdit
    UserAddedRow
    UserDeletedRow
    事件
      

  2.   

    dataGridView中自带修改纪录flg和新旧数据,你只需要把dataGridView传到后台,
    就可以在后台判断操作了.
      

  3.   

    让dataGridView和dateset绑定,然后在后台 用
    DataRowVersion.Current
    DataRowVersion.Original
    来取出修改的值,
    只需要拼写sql文就可以了
      

  4.   

     public partial class 修改资料 : Form
        {   private DataTable DT = new DataTable();
            private SqlDataAdapter SDA = new SqlDataAdapter();        public 修改资料()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
               
            }        private void button2_Click(object sender, EventArgs e)
            {            if (MessageBox.Show("确定要保存嘛?", "确认保存", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {               try
                        {
                            SqlCommandBuilder SCB = new SqlCommandBuilder(SDA);
                            SDA.Update(DT);
                            MessageBox.Show("更新成功!");
                        }
                  catch (System.Exception ex)
                        {
                            MessageBox.Show(ex.ToString());
                            return;                    }
                }        }        private void 修改资料_Load(object sender, EventArgs e)
            {            string connstring = @"server=localhost;integrated security=true;database=ps";
                string sql = @"select * from worker_table";
                SqlConnection conn = new SqlConnection(connstring);
                SqlCommand cmd = new SqlCommand(sql, conn);            SDA.SelectCommand = cmd;
                SDA.Fill(DT);
                dataGridView1.DataSource = DT;
            }  
               
        }
    大家帮我看看这个错在什么地方?每次执行都会弹出一个错误报道框,谢谢!!
      

  5.   

    SDA.SelectCommand = cmd; 
    不能单独设置select,
    把下面2行,换成1行
    SqlCommand cmd = new SqlCommand(sql, conn); 
    SDA.SelectCommand = cmd; 
    ↓↓↓↓
    SDA = new SqlDataAdapter(sql, conn);SelectCommand和UpdateCommand就都可以用了