如何从winforms datagridview中的bindingource上的第二级列表绑定列 [英] How to bind a column from second level list on bindsource in winforms datagridview
问题描述
我已经将两个实体类用于将值绑定到 DataGridView
中。一个是估计和公司。估计有Id,估计数,估计金额,CompanyId等列。我们已经创建了两个 BindingSource
,如EstimateBindingSource和CompanyBindingSource。
CompanyBindingSource将数据源作为EstimateBindingSource和DataMember作为估计
EstimateBindingSource具有数据源作为估计实体类,不定义DataMember。
我已经使用网格数据源将EstimateBindingSource绑定到Datagridview中。
在这里,我需要显示datagridview中的估计数,估计金额和公司名称..我不能实现这一点。
注意:我不执行任何代码逻辑来做到这一点..只需要使用设计来实现这一点。 / p>
请帮助我吗?
选项在DataGridView中显示二级属性
要显示导航属性的子属性,您可以使用se选项:
-
使用
DataGridViewComboBox
列并将其绑定到CompanyId
并将其设置为DataSource
到公司列表,DisplayMember
属性到名称
属性的公司和ValueMember
到Id
属性的公司。 -
覆盖
ToString()
方法公司
类并返回公司的名称
。然后在网格中显示公司
导航属性。 -
创建一个
CompanyName
属性为您的
估计
返回其Company.Name
值并显示公司名称
在网格中。 -
使用
CellFormatting
事件DataGridView
并将e.Value
设置为要在单元格中显示的所需值(公司名称)。 -
使用
Linq
查询或使用ViewModel
形成您的估算列表,并传递结果到数据网格视图。 -
为您的
估算
创建一个TypeDescriptor
键入以解析二级属性。
。
要显示公司的财产,而不是公司ID,您可以使用DataGridViewComboBoxColumn
。
使用ComboBox列
由于您要求使用设计器而不编写代码的机制这个选项更多。这是您应该执行的设置:
-
EstimatesBindingSource
应绑定到估计
-
DataGridView
应绑定到EstimatesBindingSource
-
CompanyBindingSource
仅用作组合框列的数据源,并应使用列表公司
- 在
中显示
CompanyName
/ code>列表,只需使用DataGridViewComboBoxColumn
并将其设置为DataSource
到公司列表并设置DisplayMember
至CompanyName
,它的价值成员为Id
。并绑定到CompanyId
字段估计
。
另外如果你的要求是不显示为 ComboBox
,只需设置 DisplayStyle
属性 DataGridViewComboBoxColumn
至 Nothing
。它会删除下拉样式。
您还可能会发现此信息有帮助:
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"
I have created two BindingSource
such as EstimateBindingSource and CompanyBindingSource.
CompanyBindingSource has datasource as EstimateBindingSource and DataMember as Estimates EstimateBindingSource has datasource as Estimates entity Class and no DataMember defined.
I have bound the EstimateBindingSource into the Datagridview using grid datasource.
Here, i need to show Estimate number, Estimate Amount and Company Name in datagridview.. I have't able to achieve this.
Note: I do not do any code behind logic to do this.. Need to achieve this only using design.
Could any one please help me?
Options to show Second Level Properties in DataGridView
To show a sub property of your navigation property you can use either of these options:
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.Override
ToString()
method ofCompany
class and returnName
of company. Then showCompany
navigation property in grid.Create a
CompanyName
property for yourEstimate
which returns itsCompany.Name
value and showCompanyName
in grid.Using
CellFormatting
event ofDataGridView
and sete.Value
to desired value (company name) you want to display in cell.Shape your Estimates list using a
Linq
query or use aViewModel
and pass the result to data grid view.Create a
TypeDescriptor
for yourEstimate
type to resolve second level properties. . To show a property of company instead of company id, you can use aDataGridViewComboBoxColumn
.
Using ComboBox Column
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 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
.
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:
这篇关于如何从winforms datagridview中的bindingource上的第二级列表绑定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!