简体   繁体   English

C# | WPF | DataGrid - 绑定到来自两个表的源

[英]C# | WPF | DataGrid - Binding to the sources from two Tables

I have a datagrid, named orderItemsGrid, which contains the columns (or TextColumn Headers, to prevent confusion) Item Name, Unit Price, Quantity, and Amount (Amount=Quantity*unitPrice) of each item that a customer has ordered in an order.我有一个名为 orderItemsGrid 的数据网格,其中包含客户在订单中订购的每个项目的列(或 TextColumn 标题,以防止混淆)项目名称、单价、数量和金额(Amount=Quantity*unitPrice)。 I would like to fetch the info to this grid from a SQL Express 2008 R2 Database, called Business, using LINQ to SQL.我想使用 LINQ to SQL 从名为 Business 的 SQL Express 2008 R2 数据库中获取此网格的信息。 But the problem is, the columns ItemName and Price exist in the table named ITEMS and the columns Quantity and Amount are in ORDER_ITEMS.但问题是,ItemName 和Price 列存在于名为ITEMS 的表中,而Quantity 和Amount 列存在于ORDER_ITEMS 中。

Relationships:关系:
FK_ORDER_ITEMS_ORDERS (OrderID) FK_ORDER_ITEMS_ORDERS(订单 ID)
FK_ORDER_ITEMS_ITEMS (ItemID) FK_ORDER_ITEMS_ITEMS (ItemID)

The database diagram is here:数据库图如下:

Please revert with comments.请回复评论。

You can use joins in LINQ, that is not problem, in linq query you can create new type with columns what you just need, hat will be properties in that new type, you can give them other names .您可以在 LINQ 中使用连接,这没问题,在 linq 查询中,您可以使用您需要的列创建新类型,这将是该新类型中的属性,您可以给它们其他名称。 That list, result of query, can be DataSource for your grid.该列表,即查询结果,可以是您网格的数据源。 If you need help with joins or query tell如果您在连接或查询方面需要帮助,请告诉

SOLUTION: 解:

The new code has worked, but there is one problem - not a big one though - that it fetches all attributes of both objects. 新代码已经奏效,但是存在一个问题-尽管不是一个大问题-它获取了两个对象的所有属性。 New Code: hxxp://i51.tinypic.com/13zs4ug.jpg Old Code: hxxp://i51.tinypic.com/29oppb8.jpg 新代码:hxxp://i51.tinypic.com/13zs4ug.jpg旧代码:hxxp://i51.tinypic.com/29oppb8.jpg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM