datagridview binding to entity not updating database

c# datagridview entity-framework winforms


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

enter image description here

5/15/2013 2:03:49 PM

Accepted Answer

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;
5/10/2013 10:25:33 PM

Popular Answer

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.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow