如何从绑定第二级列表bindsource中的WinForms DataGridView中列 [英] How to bind a column from second level list on bindsource in winforms datagridview

查看:211
本文介绍了如何从绑定第二级列表bindsource中的WinForms DataGridView中列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用了两个实体类绑定值到 DataGridView的。其一是估计和公司。估计有诸如标识,估算数字,估算金额,CompanyId栏目。公司拥有如编号,公司名称,地址

I have used two entity classes for binding values into DataGridView. One is Estimates and Companies. Estimates has columns such as "Id, Estimate Number, Estimate Amount, CompanyId". Companies has columns such as "Id, Company Name, Address"

我已经创建了两个的BindingSource 如EstimateBindingSource和列CompanyBindingSource。

I have created two BindingSource such as EstimateBindingSource and CompanyBindingSource.

CompanyBindingSource有数据源作为EstimateBindingSource和DataMember为估计
EstimateBindingSource有数据源的估计实体类,也没有定义数据成员。

CompanyBindingSource has datasource as EstimateBindingSource and DataMember as Estimates EstimateBindingSource has datasource as Estimates entity Class and no DataMember defined.

我一直在使用网格的数据源绑定的EstimateBindingSource到DataGridView的。

I have bound the EstimateBindingSource into the Datagridview using grid datasource.

在这里,我需要显示的估计数,估算金额和公司名称中的datagridview ..我have't能够实现这一

Here, i need to show Estimate number, Estimate Amount and Company Name in datagridview.. I have't able to achieve this.

请注意:我没有做任何代码背后的逻辑来做到这一点。需要实现这一目标只能通过设计

Note: I do not do any code behind logic to do this.. Need to achieve this only using design.

可能任何一个请帮助我吗?

Could any one please help me?

推荐答案

选项在DataGridView中显示第二级属性

要显示你的导航属性的子属性,你可以使用这些选项:

To show a sub property of your navigation property you can use either of these options:


  1. 使用 DataGridViewComboBox 列,并将其绑定到 CompanyId 并设置它的数据源来公司的名单,而的DisplayMember 属性名称公司财产和 ValueMember 标识公司的财产。

  1. Use a DataGridViewComboBox column and bind it to CompanyId and set it's DataSource to list of companies, and DisplayMember property to Name property of company and ValueMember to Id property of company.

覆盖的ToString() 公司的方式类,并返回名称公司。然后显示网格公司导航属性。

Override ToString() method of Company class and return Name of company. Then show Company navigation property in grid.

创建一个公司名称属性的估算返回其 Company.Name 价值,并显示公司名称在网格中。

Create a CompanyName property for your Estimate which returns its Company.Name value and show CompanyName in grid.

使用 CellFormatting 事件 DataGridView的并设置 e.Value 期望值要在单元格中显示(公司名称)。

Using CellFormatting event of DataGridView and set e.Value to desired value (company name) you want to display in cell.

使用的LINQ 查询塑造你的预算清单或使用视图模型和通结果数据网格视图。

Shape your Estimates list using a Linq query or use a ViewModel and pass the result to data grid view.

创建一个 TypeDescriptor 估算键入要解决第二级属性。

为了显示公司而不是公司标识的属性,你可以使用 DataGridViewComboBoxColumn

Create a TypeDescriptor for your Estimate type to resolve second level properties. . To show a property of company instead of company id, you can use a DataGridViewComboBoxColumn.

是组合框列

由于您请求的,它使用的设计师无需编写代码我描述的机制该选项的详细。这里是你应该执行设置:

Since you requested for a mechanism which uses designer without writing code I describe this option more. Here is settings you should perform:


  • EstimatesBindingSource 应绑定到列表估算

  • DataGridView的应绑定到 EstimatesBindingSource

  • CompanyBindingSource 只作为组合框列的数据源,并应使用清单填写公司

  • 要显示公司名称估算名单,这足以使用 DataGridViewComboBoxColumn 并设置它的数据源来公司的List和Set在的DisplayMember 公司名称和它的价值成员编号。并将其绑定到 CompanyId 估算的领域。

  • EstimatesBindingSource should bind to a list of Estimates
  • The DataGridView should bind to EstimatesBindingSource
  • CompanyBindingSource is only used as data source of the combo box column and should be filled using a list of Companies
  • To show CompanyName in Estimates list, it's enough to use a DataGridViewComboBoxColumn and set it's DataSource to list of companies and set the DisplayMember to CompanyName and it's value member to Id. And bind it to CompanyId field of Estimate.

另外> DataGridViewComboBoxColumn 的属性没有。这样才能消除下拉式的

Also if your requirement is to don't show it as ComboBox, simply set DisplayStyle property of DataGridViewComboBoxColumn to Nothing. It removes dropdown style.

您也可以找到这个职位有帮助:

You also may find this post helpful:

  • Show Properties of a Navigation Property in DataGridView (Second Level Properties)

这篇关于如何从绑定第二级列表bindsource中的WinForms DataGridView中列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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