DataGridView Databinding issue

C

crm

I have two interfaces InterfaceA, and InterfaceB.
InterfaceA has a readonly integer property, and a string description
("ID" and "Description" respectively..
InterfaceB has a couple miscellaneous properties and inherits from
InterfaceA.
I'm trying to create a DataGridView that will allow me to edit a list
of InterfaceA items. Everything works fine if the object I pass in only
implements InterfaceA; but if I use InterfaceB, I get an error when
clearing the datagridView:

' Assign a datasource to the combobox in the only
datagridviewcolumn
Public Sub SetListSource(ByVal values As List(Of InterfaceA))
Me.ListColumn.DisplayMember = "Description"
Me.ListColumn.ValueMember = "ID"
Me.ListColumn.ValueType = GetType(Integer)
Me.ListColumn.ReadOnly = False
Me.ListColumn.DataSource = values
End Sub

' populate the rows of the datagridviewcolumn
Public Sub SetDataSource(ByVal list As (Of InterfaceA))
Me.DataGridView.Rows.Clear()

For Each item As ICharge In list
Me.DataGridView.Rows.Add(item.ID)
Next
End Sub

I've written the following unit test as well:
Dim source as IList(of InterfaceA) = GetList()
Dim e As New InterfaceAEditor
e.SetListSource(source)
e.SetDataSource(source)

WHen I run the unit test, I get the following error:
failed: System.ArgumentException : Field called Description does not
exist.
at
System.Windows.Forms.DataGridViewComboBoxCell.InitializeDisplayMemberPropertyDescriptor(String
displayMember)
at
System.Windows.Forms.DataGridViewComboBoxCell.OnDataGridViewChanged()
at System.Windows.Forms.DataGridViewRowCollection.AddInternal(Boolean
newRow, Object[] values)
at System.Windows.Forms.DataGridView.AddNewRow(Boolean
createdByEditing)
at
System.Windows.Forms.DataGridView.OnRowCollectionChanged_PostNotification(Boolean
recreateNewRow, Boolean allowSettingCurrentCell, CollectionChangeAction
cca, DataGridViewRow dataGridViewRow, Int32 rowIndex)
at
System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged_PostNotification(CollectionChangeAction
cca, Int32 rowIndex, Int32 rowCount, DataGridViewRow dataGridViewRow,
Boolean changeIsDeletion, Boolean changeIsInsertion, Boolean
recreateNewRow, Point newCurrentCell)
at
System.Windows.Forms.DataGridViewRowCollection.OnCollectionChanged(CollectionChangeEventArgs
e, Int32 rowIndex, Int32 rowCount, Boolean changeIsDeletion, Boolean
changeIsInsertion, Boolean recreateNewRow, Point newCurrentCell)
at
System.Windows.Forms.DataGridViewRowCollection.ClearInternal(Boolean
recreateNewRow)
at System.Windows.Forms.DataGridViewRowCollection.Clear()

The error happens on the call to DataGridView.Clear().
I have no idea why. I would really appreciate any help anyone could
offer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top