通过 TableAdapter 更新数据库

Update database through TableAdapter

我在 C# 中使用 table 适配器创建了一个 DataGridView。当我双击一行时,我会以不同的形式在文本框中显示该行中的所有单元格。在我编辑文本框并单击我的保存按钮后,我想从文本框中获取值并将它们替换到数据库中。我可以在 DataGrid 上看到更改,但是我无法将它们保存在数据库中。

private void InventoryData_CellDoubleClick(object sender, DataGridViewCellEventArgs e)

{
   //make new form and display them there
   ViewInventoryItem ViewItem = new ViewInventoryItem();

   ViewItem.textBox1.Text = this.InventoryData.CurrentRow.Cells[1].Value.ToString();
   ViewItem.textBox2.Text = this.InventoryData.CurrentRow.Cells[2].Value.ToString();
   ViewItem.textBox3.Text = this.InventoryData.CurrentRow.Cells[3].Value.ToString();
   ViewItem.ShowDialog();

   if (ViewItem.DialogResult == DialogResult.OK)
   {
       //save button was pressed
       this.InventoryData.CurrentRow.Cells[0].Value = ViewItem.textBox1.Text;
       this.InventoryData.CurrentRow.Cells[1].Value = ViewItem.textBox2.Text;
       this.InventoryData.CurrentRow.Cells[2].Value = ViewItem.textBox3.Text;

       this.Validate();
       this.InventoryData.EndEdit();                 
       this.booksTableAdapter.Update(this.InventoryDataSet.Books);   
   }
             
}

找到问题了。在数据集中,Table 只有填充和获取数据。即使在选择了向导来添加其他语句之后,它还是失败了。手动添加 UpdateQuery 后,代码可以正常运行。感谢 Crowcoder 为我指明了正确的方向。