BindingSource.Add raises error

  • Thread starter Thread starter Simon
  • Start date Start date
S

Simon

I have a combobox on a form bound to a bindingSource. DataSource is
derived from BindingList as follows:

public class RendererList : BindingList<RendererListItem> {

public RendererList() {
Add(new RendererListItem("Active Server Pages"));
}

}

In my form I am calling the Add method to set the source

rendererListBindingSource.Add(_renList);

_renList is an instance of RendererList which is instantiated at declaration

ie RenderList _renList = new RenderList();

I am getting the error:

InvalidOperationException : Objects added to a BindingSource's list must
all be of the same type.

I've had this error before when I tried to add a collection to the
bindingSource which WASN'T derived from BindingList.

I appreciate this isn't srictly ADO.NET but I seem to find that the best
advice about databinding can be found in this group.

Any ideas.

Many Thanks

Simon
 
Hi,

Simon said:
I have a combobox on a form bound to a bindingSource. DataSource is derived
from BindingList as follows:

public class RendererList : BindingList<RendererListItem> {

public RendererList() {
Add(new RendererListItem("Active Server Pages"));
}

}

In my form I am calling the Add method to set the source

rendererListBindingSource.Add(_renList);

You should set the DataSource :
renderListBindingSource.DataSource = _renList;

rendererListBindingSource.Add is for adding items to the list the
rendererListBindingSource is backing.

HTH,
Greetings
 
Thanks Bart

Simon

Bart said:
Hi,






You should set the DataSource :
renderListBindingSource.DataSource = _renList;

rendererListBindingSource.Add is for adding items to the list the
rendererListBindingSource is backing.

HTH,
Greetings
 
Hi Bart,

That's got rid of my error so now the application starts and the form
displays correctly. However, if I now select the option (the only one at
the moment, although there will be more), I can't leave the combo box.
It has grabbed the focus and refuses to let go. Even attempting to close
the application with the Close Box refuses to respond.

Any ideas?

Simon
 
Hi,

Simon said:
Hi Bart,

That's got rid of my error so now the application starts and the form
displays correctly. However, if I now select the option (the only one at
the moment, although there will be more), I can't leave the combo box. It
has grabbed the focus and refuses to let go. Even attempting to close the
application with the Close Box refuses to respond.

This looks like setting e.Cancel to true inside a Validating event handler.
I don't think it's related to the DataSource, but simple bindings can do
this (Text, SelectedIndex, SelectedValue, etc):
ComboBox1.DataBindings.Add( .... );

If you have added simple bindings (designer or code), then remove them and
see if it helps.

If that doesn't help please provide more information, what are you exactly
binding and how...

HTH,
Greetings
 
Hi Bart,

Yes that's fixed it but not a full solution. Probably helps if I tell
you what I'm doing.

I have a set of controls on a form that are bound to an underlying
object instance. I have created a datasource from this object (class to
be specific) and bound the controls to this datasource. Hence the
bindingSource has been added to the component tray on the form.

This is fine for the simple control (text boxes etc). I also have a
datagridview which is bound to a collection within this underlying
object and indeed that is working fine.

I have two properties on my underlying object DatabaseRenderer and
Renderer that just take a string value. I have two combo boxes on the
form that allow the user to select options for these properties. So I'm
trying to fill the comboboxes with some values and then assign the
selectedvalue back to the underlying property. When I save and reload
this file (by serilaizing the underlying object) I want the correct
option to be selected in the combobox.

At the moment I have two collection classes derived from BindingList to
fill these combo boxes and the constructor for these collection classes
populates the item to appear in the list. I'm not precious about this
design (in fact in seems a bit flakey to me), so am open to any other
suggestion about how to fill the boxes and bind the selected item to the
underlying object.

Hope this provides enough info.

Thanks again for your help

Simon
 
Hi,

Simon said:
Hi Bart,

Yes that's fixed it but not a full solution. Probably helps if I tell you
what I'm doing.

I have a set of controls on a form that are bound to an underlying object
instance. I have created a datasource from this object (class to be
specific) and bound the controls to this datasource. Hence the
bindingSource has been added to the component tray on the form.

This is fine for the simple control (text boxes etc). I also have a
datagridview which is bound to a collection within this underlying object
and indeed that is working fine.

I have two properties on my underlying object DatabaseRenderer and
Renderer that just take a string value. I have two combo boxes on the form
that allow the user to select options for these properties. So I'm trying
to fill the comboboxes with some values and then assign the selectedvalue
back to the underlying property. When I save and reload this file (by
serilaizing the underlying object) I want the correct option to be
selected in the combobox.

At the moment I have two collection classes derived from BindingList to
fill these combo boxes and the constructor for these collection classes
populates the item to appear in the list. I'm not precious about this
design (in fact in seems a bit flakey to me),

It's not that bad, if they don't change often, otherwise you could
serialize/deserialize them too, instead of filling in the c'tor.
so am open to any other suggestion about how to fill the boxes and bind
the selected item to the underlying object.

Hope this provides enough info.

What you describe should basicly work, so something must be wrong.

1
First of all, are you using the right ValueMember and the right DataSource
for the simple binding:
(could be from designer or from code)
RendererComboBox.DataSource = rendererListBindingSource;
RendererComboBox.DisplayMember = "...";
RendererComboBox.ValueMember = "(*)";
RendererComboBox.Add("SelectedValue", masterObjectBindingSource, "Renderer")

(*) ValueMember must be set to a property that has the same type as
MasterObject.Renderer.

2
And check if there no problems with the property-get and -set for the
MasterObject.Renderer and MasterObject.DataBaseRenderer.

If you bind to "SelectedValue" and you can not move out of the ComboBox,
then a problem occured while DataBinding tries to persist SelectedValue to
the underlying object (property).


HTH,
Greetings
 
Hi Bart,

Bart said:
Hi,




It's not that bad, if they don't change often, otherwise you could
serialize/deserialize them too, instead of filling in the c'tor.




What you describe should basicly work, so something must be wrong.

1
First of all, are you using the right ValueMember and the right DataSource
for the simple binding:
(could be from designer or from code)
RendererComboBox.DataSource = rendererListBindingSource;
RendererComboBox.DisplayMember = "...";
RendererComboBox.ValueMember = "(*)";
RendererComboBox.Add("SelectedValue", masterObjectBindingSource, "Renderer")

I can't find an Add method of the ComboBox?

Simon
 
Ahhh .. still can't find the Add method, but I've found the problem. It
was to do with the property get/set.

Cheers

Simon
 
Hi,

Simon said:
Ahhh .. still can't find the Add method, but I've found the problem. It
was to do with the property get/set.

Nice :-)

I meant ComboBox.DataBindings.Add(...) (or from designer), but appearently
it was the property get and set.

Greetings
 
Back
Top