dataGridView1.DataSource = gv.GeiView().Tables["DATAWEI"];
DATAWEI 是数据库的表
这是datagridview的数据源
请问我运行程序时候直接在datagridview里修改删除,怎么能同步更新到数据库里
求代码

解决方案 »

  1.   

    用sql语句更新数据库啊,,
    更新后重新绑定列表
      

  2.   

    http://topic.csdn.net/u/20120609/16/57729ce3-fe64-425f-bd3d-1a84695e9ba3.html
    参考这里!
      

  3.   

    "不返回任何键列信息的SelectCommand不支持UpdateCommand的动态SQL生成"报这个错误我的表有主键,但是不包含在我查询出来的列中
      

  4.   

    这个我理解,只是不知道如何获得datagridview的更改
      

  5.   

    遍历一次当前表啊,如果用SQL语句更新。
      

  6.   

    光标所在行第i列的值
    dataGridView1.CurrentRow.Cells[i].Value;
    //获得光标所在行的值,存放在数组中
                      for (int i = 0; i < dataGridView1.CurrentRow.Cells.Count; i++)
                        {
                            str[i] = Convert.ToString(dataGridView1.CurrentRow.Cells[i].Value);
                        }
    //更新就把该行的数据Update
    //删除就取该行某一列的字段Delete
      

  7.   

     table A(x,y)
     private void Delete_Click(object sender, EventArgs e)
            {
                if (ConnectionState.Closed == conn.State)
                {
                    conn.Open();
                    string str = string.Format("delete from A where x='{0}'", dataGridView1.CurrentRow.Cells[0].Value);
                    SqlCommand comd = new SqlCommand();
                    comd.Connection = conn;
                    comd.CommandText = str;
                    comd.ExecuteNonQuery();
                    conn.Close();
                }
            }
        private void Update_Click(object sender, EventArgs e)
        {            if (ConnectionState.Closed == conn.State)
                {
                    conn.Open();
                    string str = string.Format("Update  A set y='{0}' where x='{1}'",dataGridView1.CurrentRow.Cells[1].Value ,dataGridView1.CurrentRow.Cells[0].Value);
                    SqlCommand comd = new SqlCommand();
                    comd.Connection = conn;
                    comd.CommandText = str;
                    comd.ExecuteNonQuery();
                    conn.Close();
                }    }
     
      

  8.   

    private void btnDel_Click(object sender, EventArgs e)
    {
    string key = this.txtCode.Text.Trim();
    foreach (DataGridViewRow row in this.dataGridView.Rows)
    {
    if (row.Cells[1].Value.ToString() == key)
    {
    Dictionary<string, object> p = new Dictionary<string, object>();
    p["主键"] = key;//执行删除sql语句//重新执行检索,更新dataGridView列表}
    }
    }