0

我已經在我的項目一直使用ObjectContext的時候,但現在我想的DbContext,我發現了一些差異是綁定到數據源時,我不明白,例如ObjectContext的和的DbContext差異結合

使用的DbContext

我綁定這樣

DbSet<Client> Clients = ctx.Client; 
Clients.Load(); 
bsClients.DataSource = Clients().Local.ToBindingList(); 
bsClientPhones.DataSource = ((Client)bsClients.Current).Phone; // this line doesn't work 

當我綁定「bsClientPhones」這樣的數據源,電網不添加時顯示的更改或刪除,直到我保存和重新加載數據。我無法將其轉換爲Local或Bindinglist。

我不知道這是否是正確的方式來使用DbContext 請有人指導我嗎?我一直在閱讀許多文檔,但我迷路了。

回答

0

您必須在實體級別實現ObservableCollection,並將其提供給網格。

public class ObservableListSource<T> : ObservableCollection<T>, IListSource 
     where T : class 
    { 
     private IBindingList _bindingList; 

     bool IListSource.ContainsListCollection { get { return false; } } 

     IList IListSource.GetList() 
     { 
      return _bindingList ?? (_bindingList = this.ToBindingList()); 
     } 
    } 

this.categoryBindingSource.DataSource = 
       _context.Categories.Local.ToBindingList(); 

每次更改後,需要調用this.categoryDataGridView.Refresh();

這裏是快速演示,我可以在DataGridView放棄。這同樣適用於GridView for Web表單。

https://github.com/codebased/tm/tree/master/WindowsFormTestConsole

+0

您提供不工作的鏈接(除非你打算現身「404絕地」網頁...) – Matt 2014-12-04 11:52:41