0
votes

I'm trying to implement a multi-select ComboBox in a DataGridView (WinForms). I've created a fairly basic OwnerDraw ComboBox-derived control, which seems to work OK. I've then hosted that control inside a DataGridView by creating a class derived from DataGridViewColumn. This all displays OK. The problem I have is with data binding to the new column.

The DataGridView DataSource is a BindingList<MyObject>. MyObject has a property, MyProperty, which is a BindingList<long>. I'm passing a separate DataSource to the MultiSelectComboBox and setting DisplayMember and ValueMember, such that the combo box acts correctly looking up the description for the values in MyProperty (ValueMember is a long identifier, DisplayMember is a string description).

The problem I have occurs when the DataGridView tries to commit the value back to the datasource. The MultiSelectComboBox exposes a SelectedValues property of type List<object>. I set the value of the cell to this value. Because the ValueMember names a long property, the object passed back is actually a List<long>. I get an InvalidCastException, where the framework attempts to cast from List<long> to BindingList<long>. This, presumably, is because the data binding is trying to set MyProperty to the cell value.

Can anybody give me some clues regarding how binding to a BindingList is supposed to work? Does the cell-hosted control (my MultiSelectComboBox) have to look at its data binding and behave differently if it is bound to a BindingList or is there some support in the framework that might help here?

1

1 Answers

0
votes

Have you tried to pass it in your BindingList constructor like this:

BindingList<long> myBindingList= new BindingList<long>(myList);