【问题标题】:DataGridView selects, but won't update or deleteDataGridView 选择,但不会更新或删除
【发布时间】:2013-10-27 04:17:44
【问题描述】:

情况是这样的。

我在 Access 中有一个数据库,已迁移到 SQL Server。在 Visual Studio 2012 (C#) 中,我使用它创建了一个数据连接,名为“stanleylabs\sqlexpress.teccartHoraire.dbo”。我有很多表和查询等。然后我创建了几个 DataGridViews。例如,我使用包含教师列表的表创建了一个 DataGridView,其中包含他们的名字和姓氏。 GridView 已创建。我更改了列,因此它们的名称将使用真实句子而不是变量,并且我检查了“激活选择”、“激活修改”、“激活删除”(请记住,我使用的是法语版本的 Visual Studio 2012,所以这些选项可能不会完全像那样命名)。我还没有输入任何代码。

尽管检查了所有这些选项,但 DataGridView 似乎允许我删除行并在表中添加数据,但并没有保存它们。当我执行时,我可以添加、删除或修改,但是说我离开了应用程序或只是离开了窗口,我所做的并没有在数据库中更新。有人可以帮我解决这个问题吗?我搜索了解决方案,但找不到任何东西...

【问题讨论】:

    标签: c# sql datagridview


    【解决方案1】:

    您必须明确更新/删除特定事件的记录。在向数据库添加数据源时,Visual Studio 会为 FormLoad 事件创建一个事件处理程序,并添加一行代码,从数据库中加载表中的所有记录,类似于:

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'yourDataSet.SalesOrderHeader' table. You can move, or remove it, as needed.
            this.salesOrderHeaderTableAdapter.Fill(this.yourDataSet.SalesOrderHeader);
        }
    

    您可以在编辑表单时按 F7 看到此代码。

    您需要做的是附加到某个事件,比如 FormClosing 并更新绑定到 DataGridView 的表:

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.salesOrderHeaderTableAdapter.Update(this.yourDataSet.SalesOrderHeader);
        }
    

    【讨论】:

    • 我完全按照你说的做了,但它仍然不会更新我的 DataGridView... private void modifyProf_FormClosing(object sender, FormClosingEventArgs e) { this.listeProfesseursTableAdapter1.Update(this.teccartHoraireDataSet.ListeProfesseurs); }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-20
    • 1970-01-01
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多