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?