如何使用 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 包含诸如 Id、Estimate Number、Estimate Amount、CompanyId" 等列.
- 公司有ID、公司名称、地址"等列
我创建了两个 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.
我已经使用网格 DataSource
将 EstimateBindingSource
绑定到 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
类的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.
使用 DataGridView
的 CellFormatting
事件并将 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
.并将其绑定到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
,只需将 DataGridViewComboBoxColumn
的 DisplayStyle
属性设置为 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屋!