如何使用 BindingSource 在 DataGridView 中绑定导航属性(二级属性)? [英] How to bind a Navigation Property (second level properties) in DataGridView using BindingSource?

查看:20
本文介绍了如何使用 BindingSource 在 DataGridView 中绑定导航属性(二级属性)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了两个实体类将值绑定到 DataGridView.一种是估计和公司.

I have used two entity classes for binding values into DataGridView. One is Estimates and Companies.

  • Estimates 包含诸如 Id、Estimate Number、Estimate Amount、CompanyId" 等列.
  • 公司有ID、公司名称、地址"等列

我创建了两个 BindingSource,例如 EstimateBindingSourceCompanyBindingSource.

I have created two BindingSource such as EstimateBindingSource and CompanyBindingSource.

  1. CompanyBindingSourceDataSource 作为 EstimateBindingSourceDataMember 作为 Estimates
  2. EstimateBindingSourceDataSource 作为 Estimates 实体类并且没有定义 DataMember.
  1. CompanyBindingSource has DataSource as EstimateBindingSource and DataMember as Estimates
  2. EstimateBindingSource has DataSource as Estimates entity Class and no DataMember defined.

我已经使用网格 DataSourceEstimateBindingSource 绑定到 DataGridView 中.

I have bound the EstimateBindingSource into the DataGridView using grid DataSource.

在这里,我需要在 DataGridView 中显示估计数字、估计金额和公司名称.我无法实现这一点.

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

注意:我没有在逻辑背后做任何代码来做到这一点..只需要使用设计来实现这一点.

推荐答案

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

要显示导航属性的子属性,您可以使用以下任一选项:

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

  1. 使用 DataGridViewComboBox 列并将其绑定到 CompanyId 并将它的 DataSource 设置为公司列表和 DisplayMember 属性到公司的 Name 属性和 ValueMember 到公司的 Id 属性.

  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.

覆盖Company 类的ToString() 方法并返回公司的Name.然后在网格中显示 Company 导航属性.

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

为您的 Estimate 创建一个 CompanyName 属性,返回其 Company.Name 值并显示 CompanyName> 在网格中.

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

使用 DataGridViewCellFormatting 事件并将 e.Value 设置为要在单元格中显示的所需值(公司名称).

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

使用 Linq 查询或使用 ViewModel 塑造您的估算列表并将结果传递给数据网格视图.

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

为您的 Estimate 类型创建一个 TypeDescriptor 以解析二级属性..要显示公司的属性而不是公司 id,您可以使用 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 应该绑定到一个 Estimates
  • 列表
  • DataGridView 应该绑定到 EstimatesBindingSource
  • CompanyBindingSource 仅用作组合框列的数据源,应使用Companies
  • 列表填充
  • 要在Estimates 列表中显示CompanyName,使用DataGridViewComboBoxColumn 并将其设置为DataSource 就足够了公司并将 DisplayMember 设置为 CompanyName 并将其值成员设置为 Id.并将其绑定到 EstimateCompanyId 字段.
  • 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.

此外,如果您的要求是不将其显示为 ComboBox,只需将 DataGridViewComboBoxColumnDisplayStyle 属性设置为 Nothing.它删除了下拉样式.

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:

这篇关于如何使用 BindingSource 在 DataGridView 中绑定导航属性(二级属性)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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