C# 想通过datagridview 更新Access的困惑
C# 想通过datagridview 更新Access,在CellEndEdit事件中编程,有时不能成功?修改完单元格以后,如果光标直接移到其它行则自动更新,如果移到本行则不能更新,需要再次修改单元格的值后才能成功,不知什么原因?
代码如下:
private void GridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
DataTable table1 = (DataTable)GridView1.DataSource;
AdapterUpdate(selectstring, table1);
}
private OleDbDataAdapter AdapterUpdate(string SelectString, DataTable tableName)
{
OleDbDataAdapter Adapter = new OleDbDataAdapter();
try
{
oleDbConnection1.Close();
oleDbConnection1.Open();
OleDbCommand command = new OleDbCommand(SelectString, oleDbConnection1);
Adapter.SelectCommand = command;
OleDbCommandBuilder builder = new OleDbCommandBuilder(Adapter);
Adapter.UpdateCommand = builder.GetUpdateCommand();
Adapter.InsertCommand = builder.GetInsertCommand();
Adapter.DeleteCommand = builder.GetDeleteCommand();
Adapter.Update(tableName);
tableName.AcceptChanges();
DataSet ds = CreateDataSet(selectstring, tablename);
GridView1.DataSource = ds.Tables[tablename];
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "出现错误");
}
return Adapter;
}
另外,通过DataTable绑定的方式也遇到同样的问题。不知何故?求指点。