简体   繁体   English

datagridview绑定到不更新数据库的实体

[英]datagridview binding to entity not updating database

I am populating a grid from a entity object and it is displaying the data fine. 我正在从实体对象填充网格,它正在显示数据。 When I make changes and save it back, nothing is updating. 当我进行更改并将其保存回来时,没有任何更新。

Here is my code: 这是我的代码:

In my load event: 在我的加载事件中:

  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();

My class: 我的课:

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; }
}

In the button to save the changes: 在保存更改的按钮中:

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

What am I doing wrong? 我究竟做错了什么?

In response to bmused: 为了回应bmused:

Defined at the class level: 在班级定义:

private SuburbanPortalEntities _entities;

defined in my load: 在我的负载中定义:

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

It is showing that it cannot load symbol Load and Local: 它显示它无法加载符号Load和Local:

在此输入图像描述

Two-way databinding with Winforms and Entity Framework can be achieved by creating an IBindinglist from the DbContext Local ObservableCollection<T> and setting it as the DataSource of a BindingSource . 可以通过从DbContext Local ObservableCollection<T>创建IBindinglist并将其设置为BindingSourceDataSource来实现与Winforms和Entity Framework的双向数据绑定。 Example: 例:

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;

You are change the properties of the projected copy of the entity, while entity itself stays unchanged. 您将更改实体的预计副本的属性,而实体本身保持不变。 That is why save does not work - entity stays unchanged. 这就是为什么保存无效 - 实体保持不变。

You need either bind entities itself as DataSource to grid, or update property of the corresponding entity when you updating property of projection instance. 您需要将实体本身作为DataSource绑定到网格,或者在更新投影实例的属性时更新相应实体的属性。

.Load().Local将是可见的,当使用参考:

 using System.Data.Entity;

You are creating the new DataBindingProjection() so we are assuming this is a class that is controlled by your context right? 您正在创建新的DataBindingProjection(),因此我们假设这是一个由您的上下文控制的类吗?

Assuming that, what i see that is missing in your code is for you to pass the new instance of DataBindingProjection to your DbContext(if using 4.2+ or to the ObjectContext if using older versions, i would recommend migrating to 5.0) 假设,我看到你的代码中缺少的是你将新的DataBindingProjection实例传递给你的DbContext(如果使用4.2+或者如果使用旧版本则传递给ObjectContext,我建议迁移到5.0)

You need to Attach() the created entities to the context before calling the SaveChanges(), i don't see this in your code. 在调用SaveChanges()之前,您需要将创建的实体附加()到上下文中,我在您的代码中看不到这一点。

The is the way for you to create new records to the database. 这是您为数据库创建新记录的方法。 If you want to change records that are at the database, you should not use the Linq approach where you create a new object, you should call the object itself, so it can have the EF proxies and be tracked by the ChangeTracker of EF. 如果要更改数据库中的记录,则不应使用创建新对象的Linq方法,应该调用对象本身,因此它可以具有EF代理并由EF的ChangeTracker跟踪。

For me it seems that you have a new class that is not tracked by EF..... 对我来说,似乎你有一个新的课程,没有被EF追踪.....

If you did something like this, then it should work (im assuming a property named Projection to be in your entities, just for an example) : 如果你做了这样的事情,那么它应该工作(我假设一个名为Projection的属性在你的实体中,只是为了一个例子):

var query = from c in _entities.PaymentTypes
         where c.CorporationId == _currentcorp.CorporationId 
         select c.Projection;

dataGridView_PaymentTypes.DataSource = query.ToList();

If you dont have that then you should do something like this: 如果你没有,那么你应该做这样的事情:

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();

After this you will be able to save it to the database. 在此之后,您将能够将其保存到数据库中。

看一下我关于绑定datagridView的帖子,该方法非常有效并且非常有用: 将datagridview绑定到数据库实体的最佳方法

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

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