从Datagridview更新记录

时间:2014-10-06 12:43:14

标签: c# datagrid updatecommand

我正在尝试更新DataGrid中的记录,但我收到以下错误消息:

  

在传递带有修改行的DataRow集合时,更新需要有效的UpdateCommand

以下是我的代码:

private void btnUpdate_Click(object sender, EventArgs e)
{
    System.Data.DataTable dt = ds.Tables["tblImpi"];
    this.impdg.BindingContext[dt].EndCurrentEdit();
    this.dataAdapter.Update(dt);

    MessageBox.Show("Update Successful");
}

1 个答案:

答案 0 :(得分:0)

试试这个:

SqlDataAdapter adapter;
DataSet ds;
public void ReadData()
{
    this.ds = new DataSet())
    this.adapter = new SqlDataAdapter(query, conString))
    adapter.Fill(thisds, "TABLE1");
    this.ds.AcceptChanges();
    this.dataGridView1.DataSource = ds.DefaultViewManager;
}

public void SaveData()
{
    using (DataSet changes = this.ds.GetChanges())
    {
        if (changes != null)
        {
            int updatedRows = this.dataAdapter.Update(changes);
            this.ds.AcceptChanges();
        }
    }
}

private void btnUpdate_Click(object sender, EventArgs e)
{
    SaveData();
}

For understanding DataGrid Control