如何从WPF数据网格中的相关表中显示数据 [英] How to display data from related tables in a WPF datagrid

查看:106
本文介绍了如何从WPF数据网格中的相关表中显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚找到我的WPF / Datagrid / Linq的方式,我试图找出如何从我的数据网格中的相关表中显示数据。例如我有3个表:




  • 客户:id,name

  • 产品:id,name

  • 订单:id,customerId,productId



我已经建立了关系DB表。我已经将表添加为C#DataSet,它创建了我的包装类。我现在想将查询绑定到几个不同的WPF数据标签,以获得以下内容:




  • 订单编号|产品名称|客户名称|

  • 客户列表|客户名称|订单总结(例如逗号分隔的字符串列表)|



如何在XAML中指定DataContext或ItemsSource来显示此信息从其他表?我正在使用 ObjectDataProvider 来提供设计时支持,如果有任何区别。



我有看到这个问题:如何将WPF Datagrid绑定到连接的表它创建一个新类来显示表的信息。这是我需要做的,而不是指定绑定?



编辑:



我已经使用上述第二种方法有一点成功。我想知道是否可以使用绑定来实现相同的事情,而不需要执行显式的选择查询来创建新的项目。我有一个查询,我映射到datagrid:

  var q = from d in dbMan.dataset.Orders 
选择新的{
id = d.id,
productName = d.ProductsRow.name,
custName = d.CustomersRow.name};

然后绑定到XAML中:

 < my:DataGrid.Columns> 
< my:DataGridTextColumn Header =idBinding ={Binding id}/>
< my:DataGridTextColumn Header =Product NameBinding ={Binding productName}/>
< my:DataGridTextColumn Header =客户名称Binding ={Binding custName}/>
< / my:DataGrid.Columns>


解决方案

我通常只将DataGrids ItemSource设置为DataTable代码背后。如果要在多个DataTable之间更改ItemSource,这比XAML容易。

  myDataGrid.ItemsSource = myTable。默认视图; 

随时我想更改我的datagrid显示的内容,我只需更新ItemsSource。这样我可以以任何方式操纵我的DataTable,然后通过DataGrid查看结果。



更新



要加入两个表中的数据,运行一个Linq join,以便在表中创建一个IE数量的字段。然后将您的IEnumerable转换回DataTable进行显示。我使用此方法将IEnumerable转换为DataTable。


I'm just finding my way with WPF/Datagrid/Linq and I'm trying to find out how to display data from a related table in my datagrid. For example I have 3 tables:

  • Customers: id, name
  • Products: id, name
  • Orders: id, customerId, productId

and I've set up the relations in the DB tables. I've added the tables as a C# DataSet which has created my wrapper classes. I now want to bind a query to a couple of different WPF datagrids to obtain the following:

  • list of orders as | Order Id | Product name | Customer Name |
  • list of customers as | Customer Name | Summary of orders (eg comma separated list of strings) |

How can I specify the DataContext or ItemsSource in my XAML to display this information from other tables? I'm using ObjectDataProviders to provide design-time support, if that makes any difference.

I've seen this question: How to bind WPF Datagrid to a joined table which creates a new class to display the information for the table. Is this how I need to do it rather than specifying a binding?

EDIT:

I've had a bit of success using the 2nd method above. I wonder though whether I can use bindings to achieve the same thing without needing to do an explicit select query to create the new items. I've got a query that I map to the datagrid:

var q = from d in dbMan.dataset.Orders
   select new { 
     id=d.id, 
     productName = d.ProductsRow.name, 
     custName = d.CustomersRow.name };

and then bind in the XAML:

<my:DataGrid.Columns>
  <my:DataGridTextColumn Header="id" Binding="{Binding id}" />
  <my:DataGridTextColumn Header="Product Name" Binding="{Binding productName}" />
  <my:DataGridTextColumn Header="Customer Name" Binding="{Binding custName}" />
</my:DataGrid.Columns>

解决方案

I typically just set the DataGrids ItemSource to a DataTable in the code behind. This is easier than XAML if you are going to be changing the ItemSource between more than one DataTable.

myDataGrid.ItemsSource = myTable.DefaultView;

Anytime I want to change what my datagrid is showing, I simply update the ItemsSource. That way I can manipulate my DataTable's in any way I want, and then see the outcome via the DataGrid.

Update

To join data from both tables run a Linq join to create an IEnumberable of fields you want in your table. Then convert your IEnumerable back to a DataTable for display. I use this method to convert an IEnumerable to a DataTable.

这篇关于如何从WPF数据网格中的相关表中显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆