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 asEstimateBindingSource
and DataMember asEstimates
EstimateBindingSource
has DataSource asEstimates
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.
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
DataGridView
should bind toEstimatesBindingSource
CompanyBindingSource
is only used as data source of the combo box column and should be filled using a list ofCompanies
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 setDisplayStyle
property ofDataGridViewComboBoxColumn
toNothing
. It removes dropdown style.You also may find this post helpful: