views:

43

answers:

3
+1  A: 

That's probably not DGV problem, but with this combo box. Show us the code that fills combo box and sets its properties.

If you are casting to Material class you should probably use SelectedItem instead of SelectedValue. (unless you exactly know what you're doing)

kubal5003
A: 

I would guess that you have an event-handler that isn't happy. What exactly does the message say?

You might also be having problems if you are adding the same Material instance to the list multiple times; since IndexOf will only find the first occurrence. This line makes me very suspicious:

mater.Add((Material)cmbMaterial.SelectedValue);

since it could potentially (on consecutive clicks / etc) do exactly this.


Note: if you used BindingList<T> instead all you'd have to doo is Add(...) - no resetting required:

field:

BindingList<Material> mater = new BindingList<Material>();

init grid:

dgvMaterial.DataSource = mater;

add item:

mater.Add(newInstance);
Marc Gravell
Thanks to all I've changed it to use a BindingList and it worked like a charm. Thanks
JaSk
A: 

If you assign data source to the DGV you should check element count - if zero then assign null. I don't know why this is the way it is, but I'm doing it in all my forms.

//I'm still analysing the rest of the code

kubal5003