DataGridView没有保存到数据库
本文关键字:数据库 保存 DataGridView | 更新日期: 2023-09-27 17:53:00
我正在创建一个简单的员工目录程序,这样用户就可以更新目录。但是,在当前向DataGridView添加数据时,它不会保存到数据库中。我知道需要this.staffTableAdapter.Update(this.dBDataSet1.Staff);
,它在我的程序中,如下所示。
我只是不确定我现在错过了什么,如果有人能帮助我,我会很感激的。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace StaffDirectory
{
public partial class Administration : Form
{
public Administration()
{
InitializeComponent();
}
private void staffBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.staffBindingSource.EndEdit();
this.staffTableAdapter.Update(this.dBDataSet1.Staff);
}
private void Administration_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'dBDataSet1.Staff' table. You can move, or remove it, as needed.
this.staffTableAdapter.Fill(this.dBDataSet1.Staff);
}
}
}
您应该添加try-catch
块并分析/向我们提供异常描述(ex.Message
):
private void staffBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
try{
this.Validate();
this.staffBindingSource.EndEdit();
this.staffTableAdapter.Update(this.dBDataSet1.Staff);
}
catch(Exception ex) {}
}
同时加入this.dBDataSet1.Staff.AcceptChanges()
;(re: https://msdn.microsoft.com/en-us/library/ceab2k93.aspx)
希望这能有所帮助。最好的祝福,