datagridview绑定到实体不更新数据库 [英] datagridview binding to entity not updating database

查看:142
本文介绍了datagridview绑定到实体不更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是我的代码:



在我的加载事件:

  var query = from c in _entities.PaymentTypes 
其中c.CorporationId == _currentcorp.CorporationId
选择
new DataBindingProjection
{
PaymentTypeId = c.PaymentTypeId,
CorporationId = c.CorporationId,
TokenId = c.TokenId,
IsActive = c.IsActive,
描述= c.Description,
CashChargeCodeType = c.CashChargeCodeType,
SortOrder = c.SortOrder,
ExcludeCreditCode = c.ExcludeCreditCodes,
已更新= c.IsUpdated,
IsAdded = c.IsAdded,
ClearUpdatedAndAdded = c.ClearUpdateAndAdded
};
dataGridView_PaymentTypes.DataSource = query.ToList();

我的课程:

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

在保存更改的按钮中:

  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;

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



解决方案

使用Winforms和Entity Framework进行双向数据绑定可以通过从 DbContext c中创建一个 IBindinglist / code> 本地 ObservableCollection< T> 并将其设置为 DataSource 一个 BindingSource 。例如:

  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;


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:

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:

解决方案

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

这篇关于datagridview绑定到实体不更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆