【问题标题】:WinForms DataGridView instantly save to Entity Framework tableWinForms DataGridView 即时保存到实体框架表
【发布时间】:2017-05-09 13:15:16
【问题描述】:

我听说这是一个简单的操作,在这里失去了情节。我有一个基本的 Winforms 应用程序,中间只有一个 DataGridView。

我在 SQL Server 中有一个与 Entity Framework db-first 连接的表。

    private void Form_Load(object sender, EventArgs e)
    {
        BindingSource bs = new BindingSource();
        Global.db.*Entity*.Load();
        bs.DataSource = Global.db.*Entity*.Local.ToBindingList() ;
        dataGridView1.DataSource = bs;
    }

现在我的理解是,当我启动它并更改任何单元格中的值时,它会在后台自动更新数据库!

但没有骰子。

任何人都知道这样做的方法,而无需每次都更新整个表(这将是一个大表)

【问题讨论】:

  • 不要拨打DataGridViewa GridViewDataGrid,反之亦然!!这是错误且令人困惑的,因为它们是不同的控件。总是用正确的名字来称呼事物!
  • 它不会自动更新数据库。它更新BindingList 的元素,这些元素是DbSet 的实体。您可以通过调用用于加载数据的DbContext 对象的SaveChange 方法来保存对数据库的更改。

标签: c# winforms entity-framework datagridview


【解决方案1】:

感谢 Reza,缺少什么..

    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        Global.db.SaveChanges();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-14
    • 2015-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多