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

[Solved] "Unable to cast" error with lists of custom objects

2 Answers 150 Views
Grid
This is a migrated thread and some comments may be shown as answers.
M
Top achievements
Rank 1
M asked on 02 Jun 2008, 03:46 PM
I'm using RadControls for ASPNET AJAX Q1 2008 and I have discovered a problem with the RadGrid and lists of custom objects. Suppose that you define these classes:

Public 

Class Parent

Private _Cusip As String

Public

Property Cusip() As String

Get

Return _Cusip

End Get

Set(ByVal value As String)

_Cusip = value

End Set

End Property

Public Sub New()

End Sub

End 

Class

Public 

Class Child1

Inherits Parent

Public Sub New()

End Sub

End 

Class

 

Public

Class Child2

Inherits Parent

Public Sub New()

End Sub

End

Class


And suppose that you create a grid like this:

<

telerik:RadGrid ID="RadGrid1" runat="server" AllowSorting="false" AutoGenerateColumns="false"

 CellPadding="2" CellSpacing="1" BorderWidth="0px" Width="500px" ShowFooter="True" OnNeedDataSource="GetData" >

 <MasterTableView >

 <Columns>

 <telerik:GridBoundColumn

HeaderText="Cusip"

 DataField="Cusip"

 ReadOnly="True" />

 </Columns>

 </MasterTableView>

 </telerik:RadGrid>

And the GetData method is defined like this:

Protected Sub GetData(ByVal sender As Object, ByVal e As GridNeedDataSourceEventArgs)

 Dim list As New List(Of Parent)

Dim a As New Child1

Dim b As New Child1

Dim c As New Child2

a.Cusip = "a"

b.Cusip = "b"

c.Cusip = "c"

list.Add(DirectCast(a, Parent))

list.Add(DirectCast(b, Parent))

list.Add(DirectCast(c, Parent))

RadGrid1.DataSource = list

End Sub

When you try viewing the page, you get the error "Unable to cast object of type 'Child2' to type Child1'."  However, if you change the code so that you add a, then c, and finally b to the list, the grid displays properly.

The problem is that when you have a list of objects of different types in the data source, the RadGrid assumes that if the first two objects are of the same type, then all of the objects are of that type. It then tries to cast all of the objects in the list to object of the first type encountered. However, if the first two objects are of different types, then it does not try casting the remaining objects to the first type. This error occurs even though the list is declared to be a list of objects of the parent type and the objects are cast to the parent type before they are added to the list.

Is a fix for this problem available?

2 Answers, 1 is accepted

Sort by
0
Iana Tsolova
Telerik team
answered on 04 Jun 2008, 12:27 PM
Hi M,

The resolution to this problem is to set the EnableLinqExpressions property of your RadGrid to false. Try it out and let us know if this helps.

Let us know if any issues arise.

Kind regards,
Iana
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
M
Top achievements
Rank 1
answered on 05 Jun 2008, 12:32 PM
Yes, that fixed the problem.
Tags
Grid
Asked by
M
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
M
Top achievements
Rank 1
Share this question
or