如何使用BindingSource在DataGridView中绑定导航属性(二级属性)? [英] How to bind a Navigation Property (second level properties) in DataGridView using BindingSource?
问题描述
我使用了两个实体类将值绑定到 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
.
-
CompanyBindingSource
具有 DataSource 作为EstimateBindingSource
, DataMember 作为Estimates
-
EstimateBindingSource
具有 DataSource 作为Estimates
实体类,没有 DataMember 定义。
CompanyBindingSource
has DataSource asEstimateBindingSource
and DataMember asEstimates
EstimateBindingSource
has DataSource asEstimates
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:
-
使用
DataGridViewComboBox
列并将其绑定到CompanyId
并将其设置为DataSource
到公司列表,DisplayMember
属性到Name
公司属性,ValueMember
到公司的Id
属性。
Use a
DataGridViewComboBox
column and bind it toCompanyId
and set it'sDataSource
to list of companies, andDisplayMember
property toName
property of company andValueMember
toId
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 ofEstimates
- The
DataGridView
should bind toEstimatesBindingSource
CompanyBindingSource
is only used as data source of the combo box column and should be filled using a list ofCompanies
- To show
CompanyName
inEstimates
list, it's enough to use aDataGridViewComboBoxColumn
and set it'sDataSource
to list of companies and set theDisplayMember
toCompanyName
and it's value member toId
. And bind it toCompanyId
field ofEstimate
.
如果您的要求不是将其显示为 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屋!