This is a migrated thread and some comments may be shown as answers.

Polymorphic DataSource

2 Answers 28 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Krzysztof
Top achievements
Rank 1
Krzysztof asked on 24 Jul 2012, 08:05 AM
Hi,
I have 4 classes:
Seminar
Debate : Seminar
Break : Seminar
Normal : Seminar

I wanted to use IEnumerable<Seminar> as a datasource for RadGrid but it keeps trying to cast them to Normal. Is there a way to stop grid from casting the types to child type (I'm guessing its taking the typeof first element in the array) or manualy specify the type it should use?

2 Answers, 1 is accepted

Sort by
0
Radoslav
Telerik team
answered on 27 Jul 2012, 08:05 AM
Hi Krzysztof,

Could you please elaborate a bit more on your scenario. What do you mean by the “RadGrid but it keeps trying to cast them to Normal”. Also how do you bind the RadGrid? What objects do you have into the IEnumerable<Seminar>? Additionally I am sending you a simple example which demonstrates that the RadGrid could be bound to IEnumerable<Seminar> where the IEnumerable<Seminar> yields objects of Normal class:
public IEnumerable<Seminar> Items
{
   get
   {
       yield return new Normal() { ID = 1, Name = "Name1", NormalName = "NormalName1" };
       yield return new Normal() { ID = 2, Name = "Name2", NormalName = "NormalName2" };
       yield return new Normal() { ID = 3, Name = "Name3", NormalName = "NormalName3" };
       yield return new Normal() { ID = 4, Name = "Name4", NormalName = "NormalName4" };
   }
}
 
void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
     RadGrid1.DataSource = Items;
}

Looking forward for your reply.

Regards,
Radoslav
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Krzysztof
Top achievements
Rank 1
answered on 27 Jul 2012, 08:09 AM
Sorry already solved, using List<Seminar> rather then IEnumerable<Seminar> solved the problem.


I Had IEnumerable<Seminar> which consisted of object: Normal, Break and Debate. I was binding it by providing grid.DataSource = seminars and then grid.DataBind

When I used this as that I got an error stating that grid could not cast Break to Normal, so it seemed as he used type Normal for the datasource rather than Seminar.
Tags
Grid
Asked by
Krzysztof
Top achievements
Rank 1
Answers by
Radoslav
Telerik team
Krzysztof
Top achievements
Rank 1
Share this question
or