修改Datagridview的属性,添加以下代码:
dataGridView1.AllowUserToAddRows = false; dataGridView1.AllowUserToDeleteRows = false; dataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;
修改OleDbDataAdapter的属性,添加以下代码:
OleDbDataAdapter1.MissingSchemaAction = MissingSchemaAction.AddWithKey;
在更新数据之前,将Datagridview中的任何更改提交到DataSet中:
this.Validate(); this.productsBindingSource.EndEdit(); this.productsTableAdapter.Update(this.chinookDataSet.Products);
在更新数据时捕获任何异常,以便在出现问题时进行处理:
try { this.productsTableAdapter.Update(this.chinookDataSet.Products); } catch (Exception ex) { MessageBox.Show("An error occurred while updating the database: " + ex.Message); }