Winforms ObservableCollection OrderBy松散同步

时间:2014-12-09 10:26:17

标签: c# winforms entity-framework observablecollection

我有一个网格,我按以下方式加载数据

没有排序的方法i

public partial class frmSalesOrderDetails : BaseForm.TfrmList
{
   // DECLARE OBJECTS 
   private BusinessObject.TransactionLayer.SalesOrderMaster SalesOrderMaster = new BusinessObject.TransactionLayer.SalesOrderMaster();
   private  ObservableCollection<BusinessObject.TransactionLayer.SalesOrderDetail> ocSalesOrderDetails { get; set; }
  // DATA BINDING TO GRID
  private void DataBind_grdSalesOrderDetail(int InvoiceId)
  {
    // FILL OBJECT  SalesOrderMaster , it is based on POCO so it also contains SalesOrderMaster.SalesOrderDetails
    SalesOrderMaster = (BusinessObject.TransactionLayer.SalesOrderMaster)(_Context.SalesOrderMaster.Where(t => t.InvoiceId == InvoiceId).FirstOrDefault());
    //Fill the Grid's datasource
    grdSalesOrderDetail.DataSource = SalesOrderMaster.SalesOrderDetails.ToBindingList().ToBindingList();
  }

有分类的Mehod 2

public partial class frmSalesOrderDetails : BaseForm.TfrmList
{
   // DECLARE OBJECTS 
   private BusinessObject.TransactionLayer.SalesOrderMaster SalesOrderMaster = new BusinessObject.TransactionLayer.SalesOrderMaster();
   private  ObservableCollection<BusinessObject.TransactionLayer.SalesOrderDetail> ocSalesOrderDetails { get; set; }
  // DATA BINDING TO GRID
  private void DataBind_grdSalesOrderDetail(int InvoiceId)
    {
       // FILL OBJECT  SalesOrderMaster , it is based on POCO so it also contains SalesOrderMaster.SalesOrderDetails
       SalesOrderMaster = (BusinessObject.TransactionLayer.SalesOrderMaster)(_Context.SalesOrderMaster.Where(t => t.InvoiceId == InvoiceId).FirstOrDefault());
      // SORT THE DATA on a temp ocSalesOrderDetails2
      ObservableCollection<BusinessObject.TransactionLayer.SalesOrderDetail> ocSalesOrderDetails2 = new ObservableCollection<BusinessObject.TransactionLayer.SalesOrderDetail>(SalesOrderMaster.SalesOrderDetails.OrderBy(t => t.STFicheLineNo));    
      // Fill ocSalesOrderDetails with ocSalesOrderDetails2's data
      ocSalesOrderDetails = new ObservableCollection<BusinessObject.TransactionLayer.SalesOrderDetail>(ocSalesOrderDetails2);
      //Fill the Grid's datasource
      grdSalesOrderDetail.DataSource = ocSalesOrderDetails.ToBindingList();
}

所以问题如下:当我使用方法1并使用以下代码删除(删除)一个SalesOrderDetail:

base._Context.SalesOrderDetail.Remove(((BusinessObject.TransactionLayer.SalesOrderDetail)((DevExpress.XtraGrid.Views.Grid.GridView)grdSalesOrderDetail.MainView).GetFocusedRow()));

全部同步,记录从Context和网格数据源中删除当我使用方法2并使用以相同方式删除时:没有更多同步,记录不会从网格数据源

请有人帮忙吗。

1 个答案:

答案 0 :(得分:0)

如果您按照此链接ObservableCollection Constructor,您将看到构造函数实际上创建了传递给它的集合的副本。因此,对于您的方法1,当您删除一行时,来自_context的SalesOrderMaster会发生变化,这会立即反映在您的对象中, SalesOrderMaster 用作数据源。

对于方法2,仍然会发生相同的情况,除了现在在内存中,您有2个相同数据的副本。 _context值仍在更新,但这与对象 ocSalesOrderDetails 无关,后者有自己的数据集用作DataSource。实际上,您的网格与此变量同步,而不是实际结果。

简单的方法是调用此方法

DataBind_grdSalesOrderDetail(int InvoiceId)
每次删除操作后

都可以保持网格的同步和排序。