Binding to Business Objects and Generic List

M

ME

I have an object we'll call it MyObject. MyObject has a property calld
MyBusinessObjects of type List<IMyBusinessObject2>. IMyBusinessObject2 is a
defined interface in my application (See below). I want to bind the
MyBusinessObjects property to a ComboBox as follows:

ComboBox cbData = new ComboBox();
BindingSource bsSource = new BindingSource();

bsSource.DataSource = MyObject.MyBusinessObjects;
cbData.DisplayMemeber = "Text";
cbData.ValueMember = "Index";

cbData.DataSource = bsSource;


When I run the code I get just the type name (like
'namespace.MyBusinessObjects') of the object in the drop down or I will get
an error that says that the field "Text" does not exist. However if I
change the object in the collection to use the base interface
(IMyBusinessObject) it works fine. How can I bind a control to a business
object that uses an extended interface?

Thanks,

Matt

---------------------------------------------------
public interface IMyBusinessObject2 : IMyBusinessObject
{

}

public interface IMyBusinessObject
{
int Index
{
get;
set;
}
string Text
{
get;
set;
}
}
 
C

Chris Jobson

ME said:
I have an object we'll call it MyObject. MyObject has a property calld
MyBusinessObjects of type List<IMyBusinessObject2>. IMyBusinessObject2 is
a defined interface in my application (See below). I want to bind the
MyBusinessObjects property to a ComboBox as follows:

ComboBox cbData = new ComboBox();
BindingSource bsSource = new BindingSource();

bsSource.DataSource = MyObject.MyBusinessObjects;
cbData.DisplayMemeber = "Text";
cbData.ValueMember = "Index";

cbData.DataSource = bsSource;


When I run the code I get just the type name (like
'namespace.MyBusinessObjects') of the object in the drop down or I will
get an error that says that the field "Text" does not exist. However if I
change the object in the collection to use the base interface
(IMyBusinessObject) it works fine. How can I bind a control to a business
object that uses an extended interface?

I've no idea why it doesn't work (though I'd very much like to know!). The
only workround I've found is rather nasty - convert the list to a
List<IMyBusinessObject> before binding, e.g.:

Converter<IMyBusinessObject2, IMyBusinessObject> conv = delegate
(IMyBusinessObject2 obj) { return obj; };
bsSource.DataSource =
MyObject.MyBusinessObjects.ConvertAll<IMyBusinessObject>(conv);

Chris Jobson
 
M

ME

So I would have to conclude that to be a bug in .NET 2 then. If say the
"Text" property was defined in IMyBusinessObject and the "Index" property
was defined in IMyBusinessObject2 one would be pretty much up the creek with
out a paddle. This has BIG implications when you need to do this type of
binding to a DataGridView.

Thanks,

Matt
 
M

ME

Could you give an example of what you mean? I don't follow how the "new"
keyword will assist.

Thanks,

Matt
 
S

SP

ME said:
I have an object we'll call it MyObject. MyObject has a property calld
MyBusinessObjects of type List<IMyBusinessObject2>. IMyBusinessObject2 is
a defined interface in my application (See below). I want to bind the
MyBusinessObjects property to a ComboBox as follows:

ComboBox cbData = new ComboBox();
BindingSource bsSource = new BindingSource();

bsSource.DataSource = MyObject.MyBusinessObjects;
cbData.DisplayMemeber = "Text";
cbData.ValueMember = "Index";

cbData.DataSource = bsSource;


When I run the code I get just the type name (like
'namespace.MyBusinessObjects') of the object in the drop down or I will
get an error that says that the field "Text" does not exist. However if I
change the object in the collection to use the base interface
(IMyBusinessObject) it works fine. How can I bind a control to a business
object that uses an extended interface?

I have run into this problem a long time ago. You will probably have to add
the declarations that you want the databinding to see to the
IMyBusinessObject2 interface with the new keyword. Some databinding does
not find the members defined in the inherited interfaces.

SP
 
S

SP

ME said:
Could you give an example of what you mean? I don't follow how the "new"
keyword will assist.

The new keyword just stops you from getting a compilation warning. Any
declarations in your IMyBusinessObject interface that you want to bind to
need to be added to your IMyBusinessObject2 interface .

SP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top