DataGridView 不保存到数据库

DataGridView not saving to database

我正在创建一个简单的员工目录程序,我正在制作它以便用户可以更新目录。但是,当前将数据添加到 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 块和 analyze/provide 我们以及异常描述 (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(); (回复:https://msdn.microsoft.com/en-us/library/ceab2k93.aspx

希望这可能有所帮助。最好的问候,