c# – datagridview绑定到实体不更新数据库

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

这是我的代码

在我的载入事件中:

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;

显示它无法加载符号加载和本地:

解决方法

可以通过从DbContext Local ObservableCollection< T>创建IBindingList来实现具有Winforms和Entity Framework的双向数据绑定.并将其设置为BindingSource的DataSource.例:
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;

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...