From: Nathan on
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?


From: Nicholas Paldino [.NET/C# MVP] on
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.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp(a)spam.guard.caspershouse.com

"Nathan" <Nathan(a)discussions.microsoft.com> wrote in message
news:3E793C1E-535B-4331-8BB2-9815D57338D8(a)microsoft.com...
> 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?
>
>


From: Nathan on
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;}
}

"Nicholas Paldino [.NET/C# MVP]" wrote:

> 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.
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - mvp(a)spam.guard.caspershouse.com
>