2014-09-22 57 views
0

我在关注本教程http://msdn.microsoft.com/pl-PL/data/jj682076。我想从我的上下文中加载特定的类别,然后创建一个DataGrid并将其与此类别的Products属性绑定。我希望能够添加,编辑和删除产品在DataGrid上 我一直在试图与此代码:EF,WinForms。将导航属性绑定到DataGrid

context.Cetegories.Where(c => c.categoryID == _category.categoryID).Select(c => c.Products).Load(); 
this.productsBindingSource.DataSource = _context.Categories.Local.ToBindingList(); 

其中productsBindingSource是我的绑定源和DataGrid是由设计器生成与绑定源作为数据源。 我该如何做到这一点? 我想也即将直接加载产品从上下文并将其绑定到数据网格

_context.Products.Where(c => c.categoryID == _category.categoryID).Load(); 

但后来当我要添加新的产品该类别直接从电网存在与FK问题,因为我不知道如何正确设置新项目。

回答

0

我自己解决了这个问题。用于加载类别产品的早期代码是正确的。问题是此代码返回Category类型的对象并从中获取products导航属性,我不得不使用另一个具有Category的绑定源作为DataSource。之后,我可以使用productBingindSourceDataSource设置为新的bs和DataMember属性设置为products导航属性的新bs。这最终允许我将特定类别的产品与DataGrid绑定。