[英]How to bind a Navigation Property (second level properties) in DataGridView using BindingSource?
I have used two entity classes for binding values into DataGridView
. 我使用了两个实体类将值绑定到
DataGridView
。 One is Estimates and Companies. 一个是估计和公司。
I have created two BindingSource
such as EstimateBindingSource
and CompanyBindingSource
. 我创建了两个
BindingSource
例如EstimateBindingSource
和CompanyBindingSource
。
CompanyBindingSource
has DataSource as EstimateBindingSource
and DataMember as Estimates
CompanyBindingSource
将DataSource作为EstimateBindingSource
,将DataMember作为Estimates
EstimateBindingSource
has DataSource as Estimates
entity Class and no DataMember defined. EstimateBindingSource
将DataSource作为Estimates
实体类,并且没有定义DataMember 。 I have bound the EstimateBindingSource
into the DataGridView
using grid DataSource
. 我已使用网格
DataSource
将EstimateBindingSource
绑定到DataGridView
。
Here, I need to show Estimate number, Estimate Amount and Company Name in DataGridView.. I have't able to achieve this. 在这里,我需要在DataGridView中显示估计数字,估计金额和公司名称 。我无法实现这一目标。
Note: I do not do any code behind logic to do this.. Need to achieve this only using design. 注意: 我没有做任何逻辑背后的代码来做到这一点。需要只使用设计来实现这一点。
Options to show Second Level Properties in DataGridView 用于在DataGridView中显示第二级属性的选项
To show a sub property of your navigation property you can use either of these options: 要显示导航属性的子属性,可以使用以下任一选项:
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. 使用
DataGridViewComboBox
列并将其绑定到CompanyId
并将其DataSource
设置为公司列表,将DisplayMember
属性设置为公司的Name
属性,将ValueMember
设置为公司的Id
属性。
Override ToString()
method of Company
class and return Name
of company. 覆盖
Company
类的ToString()
方法并返回公司Name
。 Then show Company
navigation property in grid. 然后在网格中显示
Company
导航属性。
Create a CompanyName
property for your Estimate
which returns its Company.Name
value and show CompanyName
in grid. 为
Estimate
创建一个CompanyName
属性,该属性返回其Company.Name
值并在网格中显示CompanyName
。
Using CellFormatting
event of DataGridView
and set e.Value
to desired value (company name) you want to display in cell. 使用
DataGridView
CellFormatting
事件并将e.Value
设置为要在单元格中显示的所需值(公司名称)。
Shape your Estimates list using a Linq
query or use a ViewModel
and pass the result to data grid view. 使用
Linq
查询ViewModel
您的Estimates列表或使用ViewModel
并将结果传递给数据网格视图。
Create a TypeDescriptor
for your Estimate
type to resolve second level properties. 为
Estimate
类型创建TypeDescriptor
以解析二级属性。 . 。 To show a property of company instead of company id, you can use a
DataGridViewComboBoxColumn
. 要显示公司的属性而不是公司ID,可以使用
DataGridViewComboBoxColumn
。
Using ComboBox Column 使用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
should bind to a list of Estimates
EstimatesBindingSource
应绑定到Estimates
列表 DataGridView
should bind to EstimatesBindingSource
DataGridView
应绑定到EstimatesBindingSource
CompanyBindingSource
is only used as data source of the combo box column and should be filled using a list of Companies
CompanyBindingSource
仅用作组合框列的数据源,应使用Companies
列表填充 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
. Estimates
列表中显示CompanyName
,只需使用DataGridViewComboBoxColumn
并将其DataSource
设置为公司列表,并将DisplayMember
设置为CompanyName
,将其value值设置为Id
。 And bind it to CompanyId
field of Estimate
. Estimate
CompanyId
字段。 Also if your requirement is to don't show it as ComboBox
, simply set DisplayStyle
property of DataGridViewComboBoxColumn
to Nothing
. 此外,如果您的要求是不将其显示为
ComboBox
,只需将DataGridViewComboBoxColumn
DisplayStyle
属性设置为Nothing
。 It removes dropdown style. 它删除了下拉样式。
You also may find this post helpful: 你也可以发现这篇文章有用:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.