繁体   English   中英

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块并为我们分析/提供异常描述( 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

希望这会有所帮助。 最好的祝福,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM