DataGridView ComboBox Binding

G

Guest

Hi,

I have a datagridview bound to a List of objects (ObjectA). Each ObjectA
contains an ObjectB property.

Class ObjectA
{
public ObjectB objB {}
}

The datagridview has a combobox column that is bound to each ObjectA's
ObjectB, like this:

List<ObjectA> ObjectAList = new List<ObjectA>();
....
dgv.DataSource = ObjectAList;
dgv.Columns[1].DataPropertyName = "objB";

When the datagridview loads, I get an error in the form of a message box for
each row that loads:

The following exception occurred in the DataGridView:
System.FormatException: DataGridViewComboBoxCell value is not valid.

The comboboxes all load with the right values, but this error appears as
they load and whenever the datagridview is clicked.

Does anyone know what would cause this behavior?
 
N

Nicholas Paldino [.NET/C# MVP]

Nathan,

You can not set the expression for a binding to be a method, which is
what objB is. You have to expose it as a property.

Hope this helps.
 
G

Guest

objB is a property, as I specified in the OP. To be more specific:

private ObjectB m_objB;
public ObjectB objB
{
get {return m_objB;}
set {m_objB = value;}
}
 

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