Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataBinding to collection of Interfaces

Status
Not open for further replies.

crazyboybert

Programmer
Jun 27, 2001
798
0
0
GB
Hi All

Ive got a requirement to bind a datagrid to a collection of objects of different type. They all implement the same interface and the DataSource is a custom collection derived from collection base of IMyInterface objects. The grid only uses properties from the interface to bind to and yet when bound throws the following exception:

System.Reflection.TargetException: Object does not match target type

So, I'm guessing the databind grabs the first object from the collection, uses reflection to find its most derived type and then assumes all of the objects in the collection are this type...?

So in this case no binding to base types or interfaces at all :-( or am i missing something. Never tried to do this before, always used custom types for collections when databinding.

Any good ideas short of handling the binding manually myself or something nasty like converting my collection into a datatable or suchlike?

Cheers

Rob

i'm a boy, called Bert, and I may not be crazy, but if i'm not the rest of you are...
 
As a rather charmless fix I have created a new concrete type which implements the interface and takes an object which implements the interface in its constructor, giving a concrete "version" of the interface. Now I am then converting my collection of IMyInterface to an MyConcereteInterface[] array. It works, but I don't like it that much...

Any one with better ideas please shout.

Is it just me or does DataBind need an overload a bit like DataBind(Type t) so you can tell the binding what type to cast the objects in the collection too? Is this coming in 2.0 - I haven't checked...or is this something Generics will solve (Don't think so but maybe wrong) or am I just trying to be to clever (probably ^^)

Rob

i'm a boy, called Bert, and I may not be crazy, but if i'm not the rest of you are...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top