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

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

问题描述

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

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.


  1. CompanyBindingSource 具有 DataSource 作为 EstimateBindingSource DataMember 作为 Estimates

  2. EstimateBindingSource 具有 DataSource 作为 Estimates 实体类,没有 DataMember 定义。

  1. CompanyBindingSource has DataSource as EstimateBindingSource and DataMember as Estimates
  2. EstimateBindingSource has DataSource as Estimates entity Class and no DataMember defined.

我将 EstimateBindingSource 绑定到使用网格 DataSource 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 类的code> ToString()方法并返回公司的名称 。然后在网格中显示 Company 导航属性。

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

创建 CompanyName 估价的code>属性,该属性返回其 Company.Name 的值并显示网格中的公司名称

使用<$ c $的 CellFormatting 事件c> DataGridView 并将 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.

为您的估价创建 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.

使用ComboBox列

由于您要求使用设计器而不编写代码的机制,我描述了此选项更多。以下是您应执行的设置:

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 仅用作组合框列的数据源,并应使用公司

  • 估计中显示 CompanyName 列表,使用 DataGridViewComboBoxColumn 并将其设置为 DataSource 足以列出公司并进行设置 DisplayMember CompanyName ,并且它是 Id 的值成员。并将其绑定到 Estimate 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.

如果您的要求不是将其显示为 ComboBox ,只需设置 DisplayStyle DataGridViewComboBoxColumn 的属性设置为 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:

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

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

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