Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted
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?

Amir Syafrudin
Posted (edited)

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?

Edited by rot13
Posted
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?
Amir Syafrudin
Posted (edited)

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.

Edited by rot13
Posted

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?

  • 4 weeks later...
  • 3 years later...
Posted

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'.

Posted

Solution found.

 

To the second class (the one bound to the combobox) add this:

public 'ClassName' Self

{

get

{ return this; }

}

And set the combobox's valuemember to Self

comboBox.ValueMember = "Self";

 

Found the solution here.

Credit goes to whoever wrote that. ^^

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...