I have a GUI which allows the user to select a report to view/edit. When the user selects a report, it shows the Items in the report. The Item has many properties - most of which are binding properly. One of the properties is Owner, and this is bound to a ComboBoxColumn.
- Report
- Items
- Owner
- Items
I have done something very similar to this a few times and had no problems when I set the DataPropertyName
, DataSource
, ValueMember
, and DisplayMember
. The only difference is that this time instead of the Item
type having an OwnderID
it actually has an instance of the Owner
object.
I saw a suggestion on another post to solve this issue by giving the items bound in the list a self-referencing property that allows them to return themselves for the purposes of setting the ValueMember
However, When I bind it this way:
OwnerColumn.DataPropertyName = "Owner"
OwnerColumn.DataSource = ownersBindingSource1
OwnerColumn.ValueMember = "Self"
OwnerColumn.DisplayMember = "OwnerName"
I get a lot of errors like:
Unable to cast object of type 'System.String' to type 'Owner'.
and:
The following exception occurred in the DataGridView:
System.ArgumentException: DataGridViewComboBoxCell value is not valid.
To replace this default dialog please handle the DataError event.
I was able to get around some of these errors by binding it like this:
OwnerColumn.DataPropertyName = "Owner"
OwnerColumn.DataSource = ownersBindingSource1
and also by making the ToString
function on the Owner
display the OwnerName
property. This seems pretty hacky though - and I think I'm misunderstanding something fundamental as it still does not function properly. Any help would be much appreciated.