简体   繁体   English

如何使用BindingSource绑定DataGridView中的导航属性(二级属性)?

[英]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. 一个是估计和公司。

  • Estimates has columns such as "Id, Estimate Number, Estimate Amount, CompanyId" . 估算有“Id,Estimate Number,Estimate Amount,CompanyId”等列
  • Companies has columns such as "Id, Company Name, Address" 公司有“Id,Company Name,Address”等栏目

I have created two BindingSource such as EstimateBindingSource and CompanyBindingSource . 我创建了两个BindingSource例如EstimateBindingSourceCompanyBindingSource

  1. CompanyBindingSource has DataSource as EstimateBindingSource and DataMember as Estimates CompanyBindingSourceDataSource作为EstimateBindingSource ,将DataMember作为Estimates
  2. EstimateBindingSource has DataSource as Estimates entity Class and no DataMember defined. EstimateBindingSourceDataSource作为Estimates实体类,并且没有定义DataMember

I have bound the EstimateBindingSource into the DataGridView using grid DataSource . 我已使用网格DataSourceEstimateBindingSource绑定到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: 要显示导航属性的子属性,可以使用以下任一选项:

  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. 使用DataGridViewComboBox列并将其绑定到CompanyId并将其DataSource设置为公司列表,将DisplayMember属性设置为公司的Name属性,将ValueMember设置为公司的Id属性。

  2. Override ToString() method of Company class and return Name of company. 覆盖Company类的ToString()方法并返回公司Name Then show Company navigation property in grid. 然后在网格中显示Company导航属性。

  3. Create a CompanyName property for your Estimate which returns its Company.Name value and show CompanyName in grid. Estimate创建一个CompanyName属性,该属性返回其Company.Name值并在网格中显示CompanyName

  4. Using CellFormatting event of DataGridView and set e.Value to desired value (company name) you want to display in cell. 使用DataGridView CellFormatting事件并将e.Value设置为要在单元格中显示的所需值(公司名称)。

  5. Shape your Estimates list using a Linq query or use a ViewModel and pass the result to data grid view. 使用Linq查询ViewModel您的Estimates列表或使用ViewModel并将结果传递给数据网格视图。

  6. 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列表
  • The 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列表填充
  • 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 . 要在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.

相关问题 如何使用BindingSource在DataGridView的单列中绑定Navigation Property(二级模型的两个属性)? - How to bind Navigation Property (second level model's two properties) in DataGridView's single column using BindingSource? 在 DataGridView 中显示导航属性的属性(二级属性) - Show Properties of a Navigation Property in DataGridView (Second Level Properties) 使用ComboBox在DataGridView中显示二级属性 - Using ComboBox to show second-level properties in DataGridView 使用DataGridView.DataSource属性和BindingSource填充DataGridView - Populating DataGridView using DataGridView.DataSource property and BindingSource 将嵌套对象绑定到DataGridView BindingSource - Bind nested object to DataGridView BindingSource 如何通过BindingSource映射DataGridView中内部/嵌套类的属性? - How to map properties of inner/nested classes in DataGridView through BindingSource? 为什么AutoMapper不使用ProjectTo映射第二级导航属性? - Why AutoMapper is not Mapping mapping second level navigation property using ProjectTo? 如何将DataGrid绑定到数据源的第二级属性? - How to bind DataGrid to a second-level property of a data source? 使用BindingSource绑定到嵌套属性 - 或者,使实体可绑定 - Using BindingSource to bind to Nested Properties - or, Making Entities Bindable 如何将 DataGridView 绑定到特定属性? - How to bind DataGridView to specific properties?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM