【问题标题】:datagridview binding to entity not updating databasedatagridview 绑定到实体不更新数据库
【发布时间】:2013-05-10 20:48:00
【问题描述】:

我正在从一个实体对象填充一个网格,它可以很好地显示数据。当我进行更改并将其保存回来时,没有任何更新。

这是我的代码:

在我的加载事件中:

  var query = from c in _entities.PaymentTypes
              where c.CorporationId == _currentcorp.CorporationId
              select
                new DataBindingProjection
                  {
                    PaymentTypeId = c.PaymentTypeId,
                    CorporationId = c.CorporationId,
                    TokenId = c.TokenId,
                    IsActive = c.IsActive,
                    Description = c.Description,
                    CashChargeCodeType = c.CashChargeCodeType,
                    SortOrder = c.SortOrder,
                    ExcludeCreditCode = c.ExcludeCreditCodes,
                    IsUpdated = c.IsUpdated,
                    IsAdded = c.IsAdded,
                    ClearUpdatedAndAdded = c.ClearUpdateAndAdded
                  };
  dataGridView_PaymentTypes.DataSource = query.ToList();

我的班级:

private class DataBindingProjection
{
  public Guid PaymentTypeId { get; set; }
  public Guid CorporationId { get; set; }
  public Guid TokenId { get; set; }
  public bool IsActive { get; set; }
  public string Description { get; set; }
  public int CashChargeCodeType { get; set; }
  public int SortOrder { get; set; }
  public int ExcludeCreditCode { get; set; }
  public bool IsUpdated { get; set; }
  public bool IsAdded { get; set; }
  public bool ClearUpdatedAndAdded { get; set; }
}

在按钮中保存更改:

private void button_SaveChanges2_Click(object sender, EventArgs e)
{
  button_SaveChanges2.Enabled = false;
  _entities.SaveChanges();
  timer1.Enabled = true;
  button_SaveChanges2.Enabled = true;
}

我做错了什么?

回复 bmused:

在类级别定义:

private SuburbanPortalEntities _entities;

在我的负载中定义:

  var bs = new BindingSource();
  _entities.PaymentTypes.Where(x => x.CorporationId == _currentcorp.CorporationId).Load;
  bs.DataSource = _entities.PaymentTypes.Local.ToBindingList();
  dataGridView_PaymentTypes.DataSource = bs;

显示无法加载符号Load和Local:

【问题讨论】:

  • 为什么要投影到具有与您的实体完全相同的属性的另一种类型?
  • 测试,我尝试了几种不同的想法,最终得到了这个。当然不需要,但我离开了。
  • 不应该LoadLoad() 吗?
  • 是的...但是无论有没有它们都找不到负载,因此 () 是否找不到它并不重要。

标签: c# winforms entity-framework datagridview


【解决方案1】:

可以通过从DbContext Local ObservableCollection<T> 创建IBindinglist 并将其设置为DataSourceDataSource 来实现与Winforms 和实体框架的双向数据绑定。示例:

private BindingSource bs = new BindingSource();
private MyDbContext context = new MyDbContext();

context.MyEntities.Where(x=>x.SomeProperty == 2).Load(); 
bs.DataSource = context.MyEntities.Local.ToBindingList(); 
myDataGridView.DataSource = bs;

【讨论】:

  • 对不起,我没有早点回来,我生病了。无论如何,它说无法解析符号加载或本地。我会把我的代码贴在上面供你查看。
  • @ErocM 什么版本的实体框架?以上应该适用于 EF 4.1+,因为它使用 DbContext API 而不是旧的 ObjectContext API。还要检查您是否引用了System.Data.Entity
  • v4.0.30319 你在哪里以及如何获得 4.1+ ?我正在使用 .net 4.0 完整版。
  • @ErocM 你可以得到最新版本的nuget包msdn.microsoft.com/en-us/data/ee712906
  • ok 得到了 ef 5.0 并且能够为 Load 加载扩展,但它没有找到本地。有什么建议吗?
【解决方案2】:

您正在更改实体的投影副本的属性,而实体本身保持不变。这就是保存不起作用的原因 - 实体保持不变。

您需要将实体本身作为 DataSource 绑定到网格,或者在更新投影实例的属性时更新相应实体的属性。

【讨论】:

    【解决方案3】:

    .Load().Local 在使用引用时将可见:

     using System.Data.Entity;
    

    【讨论】:

      【解决方案4】:

      您正在创建新的 DataBindingProjection(),所以我们假设这是一个由您的上下文控制的类,对吧?

      假设,我看到您的代码中缺少的是让您将 DataBindingProjection 的新实例传递给您的 DbContext(如果使用 4.2+ 或 ObjectContext,如果使用旧版本,我建议迁移到 5.0)

      在调用 SaveChanges() 之前,您需要将创建的实体 Attach() 到上下文中,我在您的代码中没有看到这一点。

      这是您在数据库中创建新记录的方法。如果要更改数据库中的记录,则不应使用创建新对象的 Linq 方法,应调用对象本身,以便它可以具有 EF 代理并由 EF 的 ChangeTracker 跟踪。

      对我来说,您似乎有一个未被 EF 跟踪的新课程.....

      如果你做了这样的事情,那么它应该可以工作(我假设一个名为 Projection 的属性在你的实体中,仅作为示例):

      var query = from c in _entities.PaymentTypes
               where c.CorporationId == _currentcorp.CorporationId 
               select c.Projection;
      
      dataGridView_PaymentTypes.DataSource = query.ToList();
      

      如果你没有,那么你应该这样做:

      var query = from c in _entities.PaymentTypes
               where c.CorporationId == _currentcorp.CorporationId 
               new DataBindingProjection
                    {
                      PaymentTypeId = c.PaymentTypeId,
                      CorporationId = c.CorporationId,
                      TokenId = c.TokenId,
                      IsActive = c.IsActive,
                      Description = c.Description,
                      CashChargeCodeType = c.CashChargeCodeType,
                      SortOrder = c.SortOrder,
                      ExcludeCreditCode = c.ExcludeCreditCodes,
                      IsUpdated = c.IsUpdated,
                      IsAdded = c.IsAdded,
                      ClearUpdatedAndAdded = c.ClearUpdateAndAdded
                    };
      
      foreach(var item in query)
          (DbContext)YourInstanceOfContext.Set<DataBindingProjection>().Add(item);
      
      dataGridView_PaymentTypes.DataSource = query.ToList();
      

      之后,您将能够将其保存到数据库中。

      【讨论】:

        【解决方案5】:

        看看我关于绑定datagridView的帖子,这个方法效果很好,也很有帮助:Best approach to bind a datagridview to database entity/ies

        【讨论】:

          猜你喜欢
          • 2014-02-02
          • 2012-09-11
          • 2011-02-02
          • 2011-11-23
          • 2011-05-30
          • 1970-01-01
          • 2010-11-26
          • 2011-07-28
          • 1970-01-01
          相关资源
          最近更新 更多