如何从winforms datagridview中的bindingource上的第二级列表绑定列 [英] How to bind a column from second level list on bindsource in winforms datagridview

查看:189
本文介绍了如何从winforms datagridview中的bindingource上的第二级列表绑定列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将两个实体类用于将值绑定到 DataGridView 中。一个是估计和公司。估计有Id,估计数,估计金额,CompanyId等列。我们已经创建了两个 BindingSource ,如EstimateBindingSource和CompanyBindingSource。



CompanyBindingSource将数据源作为EstimateBindingSource和DataMember作为估计
EstimateBindingSource具有数据源作为估计实体类,不定义DataMember。



我已经使用网格数据源将EstimateBindingSource绑定到Datagridview中。



在这里,我需要显示datagridview中的估计数,估计金额和公司名称..我不能实现这一点。



注意:我不执行任何代码逻辑来做到这一点..只需要使用设计来实现这一点。 / p>

请帮助我吗?

解决方案

选项在DataGridView中显示二级属性



要显示导航属性的子属性,您可以使用se选项:


  1. 使用 DataGridViewComboBox 列并将其绑定到 CompanyId 并将其设置为 DataSource 到公司列表, DisplayMember 属性到名称属性的公司和 ValueMember Id 属性的公司。


  2. 覆盖 ToString()方法公司类并返回公司的名称。然后在网格中显示公司导航属性。


  3. 创建一个 CompanyName 属性为您的估计返回其 Company.Name 值并显示公司名称在网格中。


  4. 使用 CellFormatting 事件 DataGridView 并将 e.Value 设置为要在单元格中显示的所需值(公司名称)。


  5. 使用 Linq 查询或使用 ViewModel 形成您的估算列表,并传递结果到数据网格视图。


  6. 为您的估算创建一个 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:

  1. 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.

  2. Override ToString() method of Company class and return Name of company. Then show Company navigation property in grid.

  3. Create a CompanyName property for your Estimate which returns its Company.Name value and show CompanyName in grid.

  4. Using CellFormatting event of DataGridView and set e.Value to desired value (company name) you want to display in cell.

  5. Shape your Estimates list using a Linq query or use a ViewModel and pass the result to data grid view.

  6. 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.

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 of Estimates
  • The DataGridView should bind to EstimatesBindingSource
  • CompanyBindingSource is only used as data source of the combo box column and should be filled using a list of Companies
  • To show 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. And bind it to CompanyId field of Estimate.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆