[英]Display single data member of related data using Entity Framework in WPF
I have two related entities in Entity Framework, Stock
and then Suppliers
in which one stock entity can have one supplier and a supplier can have many stocks items. 我在实体框架中有两个相关实体,即Stock
,然后是Suppliers
,其中一个库存实体可以有一个供应商,而一个供应商可以有很多库存项目。
I have a data grid in which i want to display all the data about Stock such as the Id, Name, Price etc and include the SuppliersId
for it. 我有一个数据网格,我想在其中显示有关库存的所有数据,例如ID,名称,价格等,并包括它的SuppliersId
。
In the screenshot below I want it to display the SuppliersId
where it says Supplier but i cannot get it so it doesn't include just the string of the entire object. 在下面的屏幕快照中,我希望它在显示“ SuppliersId
地方显示SuppliersId
,但我无法获取它,因此它不仅仅包含整个对象的字符串。
The stock class 股票类别
class Stock
{
public Stock()
{
this.SalesInvolvedIn = new HashSet<Sales>();
}
public int StockId { get; set; }
public string StockName { get; set; }
public int StockPrice { get; set; }
public string StockType { get; set; }
public ICollection<Sales> SalesInvolvedIn { get; set; }
public Suppliers Suppliers { get; set; }
}
Fetching the data 取得资料
public List<Stock> GetStock()
{
return StockManagementDatabaseContext.Stocks.Include("Suppliers").ToList();
}
Updating the data grid 更新数据网格
private void UpdateStockList()
{
List<Stock> stocklist = repository.GetStock();
this.StockDataGrid.ItemsSource = stocklist;
}
How would I go about doing this? 我将如何去做呢?
EDIT: Showing the Supplier Class 编辑:显示供应商类别
class Suppliers
{
public int SuppliersId { get; set; }
public string SupplierName { get; set; }
public string ContactPerson { get; set; }
public int PhoneNumber { get; set; }
public ICollection<Stock> StockSold { get; set; }
}
If I understood correctly you just want to show the supplier Id? 如果我理解正确,您只想显示供应商ID? Two approaches come to mind: ViewModel
or not using autogenerated columns in the datagrid in XAML. 我想到两种方法: ViewModel
或不使用XAML的datagrid中的自动生成的列。
You could create a ViewModel
like this: 您可以这样创建一个ViewModel
:
public class StockViewModel
{
public int StockId { get; set; }
public string StockName { get; set; }
public int StockPrice { get; set; }
//etc....
public int SupplierId { get; set; }
}
Fetch the data to it: 获取数据:
public List<StockViewModel> GetStock()
{
return StockManagementDatabaseContext.Stocks.Include("Suppliers")
.Select(a => new StockViewModel
{
StockId = a.StockId,
StockName = a.StockName,
StockPrice = a.StockPrice,
//and any other you may wish to include
SupplierId = a.Suppliers.SuppliersId
}).ToList();
}
Load to your grid 加载到网格
private void UpdateStockList()
{
this.StockDataGrid.ItemsSource = repository.GetStock();
}
Alternatively you can choose what you want your grid to bind in XAML by setting AutoGenerateColumns
to False 另外,您可以通过将AutoGenerateColumns
设置为False来选择要在XAML中绑定网格的对象
<DataGrid Name="StockDataGrid"
ItemsSource="{Binding}"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding StockId }"></DataGridTextColumn>
<DataGridTextColumn Header="Name" Binding="{Binding StockName }"></DataGridTextColumn>
<DataGridTextColumn Header="Price" Binding="{Binding StockPrice}"></DataGridTextColumn>
<DataGridTextColumn Header="Supplier ID" Binding="{Binding Suppliers.SupplierId}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.