467,135 Members | 1,195 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,135 developers. It's quick & easy.

DataGridView ComboBox Binding

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?
Apr 7 '06 #1
  • viewed: 16016
Share:
2 Replies
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]
- mv*@spam.guard.caspershouse.com

"Nathan" <Na****@discussions.microsoft.com> wrote in message
news:3E**********************************@microsof t.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?

Apr 7 '06 #2
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]
- mv*@spam.guard.caspershouse.com

Apr 10 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by amber | last post: by
reply views Thread by BiT | last post: by
reply views Thread by Dom | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.