DataGridView ComboBox Cell With DataBinding

rot13

Regular
Joined
Jun 12, 2004
Messages
53
I have a DataGridView with a BindingSource bound to a BindingList<LineItem> (A custom business object that represents a line in a purchase order)

Fields in the LineItem class include Number, Product (of type Product), Quantity, and so on.

I have the product field set up as a combobox field with a binding source bound to a BindingList<Product>

The combobox dropdown populates fine, and you can select a product, but when you move off of the cell to 'commit' it, the value goes away and doesn't get set in the underlying LineItem object.

Any ideas why this happens or how to fix it?

Thanks
 
The combobox dropdown populates fine, and you can select a product, but when you move off of the cell to 'commit' it, the value goes away and doesn't get set in the underlying LineItem object.

What do you mean by moving off of the cell? Commit? Which value is gone? What are you trying to set in the LineItem object?
 
The way I understand it, is the DataGridView doesn't try to 'commit' the value to the underlying object until you move off of the cell, or row. That may not be right, but that's not really important.

I'm trying to change the Product property on the underlying LineItem object, and it will let me select the Product from the ComboBox, but when I move off of that Cell (click another Cell or another control, or hit enter) the value will go back to null.

EDIT: I just remembered that I had put in an error handler in the DataError event to keep it from showing the message box each time. When I try to change the Product property, I get an ArgumentException with the following message: Object of type 'System.String' cannot be converted to type 'Product'. It seems to think the objects in the ComboBox are strings, not Products... but they are products. Anyone have any ideas?
 
Last edited:
You said the combobox populates correctly. But what about the DataGridView. Did it bind correctly? Did every row (which represent LineItem) in the DataGridView select the correct Product in the combobox when binding?
 
Yes, when I have a Purchase Order that already has LineItems, and I open it, the DataGridView binds correctly and displays the correct product.

EDIT: Also, I tried taking the DisplayMember off of the ComboBox cell (it was 'Name' so it would show the product name there) and now when I try to create a line item and select a product, it will select the product, but I can't move focus to anything else until I hit escape to cancel the selection.
 
Last edited:
No problem. It's a tough one.

I've been all over msdn.com and just about any other website that google can find 'DataBinding' on... and still... nothing.

*sigh*

I'm just hoping somebody here has tried to do the same thing and figured out how...

anyone?
 
Any solution found?
I currently have the exact same problem.

Datagrid bound to a class containing a comboboxcell bound to an other class, wich is to be a property of the first class.
When I try to validate my combobox it tries to either use the displaymember or the if set, the valuemember instead of the object itself. Resulting in an error stating string (or if valuemember is set Int32) cannot be converted to 'classname'.
 
Back
Top