如何从绑定第二级列表bindsource中的WinForms DataGridView中列 [英] How to bind a column from second level list on bindsource in winforms datagridview
问题描述
我已经使用了两个实体类绑定值到 DataGridView的
。其一是估计和公司。估计有诸如标识,估算数字,估算金额,CompanyId栏目。公司拥有如编号,公司名称,地址
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有数据源作为EstimateBindingSource和DataMember为估计
EstimateBindingSource有数据源的估计实体类,也没有定义数据成员。
CompanyBindingSource has datasource as EstimateBindingSource and DataMember as Estimates EstimateBindingSource has datasource as Estimates entity Class and no DataMember defined.
我一直在使用网格的数据源绑定的EstimateBindingSource到DataGridView的。
I have bound the EstimateBindingSource into the Datagridview using grid datasource.
在这里,我需要显示的估计数,估算金额和公司名称中的datagridview ..我have't能够实现这一
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?
推荐答案
选项在DataGridView中显示第二级属性
要显示你的导航属性的子属性,你可以使用这些选项:
To show a sub property of your navigation property you can use either of these options:
-
使用
DataGridViewComboBox
列,并将其绑定到CompanyId
并设置它的数据源
来公司的名单,而的DisplayMember
属性名称
公司财产和ValueMember
到
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.
覆盖的ToString()
公司的方式
类,并返回名称
公司。然后显示网格公司
导航属性。
Override ToString()
method of Company
class and return Name
of company. Then show Company
navigation property in grid.
创建一个公司名称
属性的估算
返回其 Company.Name
价值,并显示公司名称
在网格中。
Create a CompanyName
property for your Estimate
which returns its Company.Name
value and show CompanyName
in grid.
使用 CellFormatting
事件 DataGridView的
并设置 e.Value
期望值要在单元格中显示(公司名称)。
Using CellFormatting
event of DataGridView
and set e.Value
to desired value (company name) you want to display in cell.
使用的LINQ
查询塑造你的预算清单或使用视图模型
和通结果数据网格视图。
Shape your Estimates list using a Linq
query or use a ViewModel
and pass the result to data grid view.
创建一个 TypeDescriptor
您估算
键入要解决第二级属性。
。
为了显示公司而不是公司标识的属性,你可以使用 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
应绑定到列表估算
- 的
DataGridView的
应绑定到EstimatesBindingSource
-
CompanyBindingSource
只作为组合框列的数据源,并应使用清单填写公司
- 要显示
公司名称
在估算
名单,这足以使用DataGridViewComboBoxColumn
并设置它的数据源
来公司的List和Set在的DisplayMember
到公司名称
和它的价值成员编号
。并将其绑定到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
.
另外> DataGridViewComboBoxColumn
的属性没有
。这样才能消除下拉式的
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)
这篇关于如何从绑定第二级列表bindsource中的WinForms DataGridView中列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!