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

[Solved] Unable to cast object of type 'Type2' to type 'Type3'

6 Answers 152 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Josemi
Top achievements
Rank 1
Josemi asked on 14 Nov 2008, 02:53 PM
Hi,

My scenario has two types, 'Type2' and 'Type3', that extends from 'Type1'. I'm trying to set a 'Type1' list with 'Type2' and 'Type3' objects as a DataSource to grid and i'm getting the following exception message:

"Unable to cast object of type 'Type2' to type 'Type3'."

Is this a bug or i missed something ?

Thanks.
Josemi

6 Answers, 1 is accepted

Sort by
0
Adam Nelson
Top achievements
Rank 2
answered on 14 Nov 2008, 10:14 PM
I've had this issue when using a standard gridview and BoundColumns

To get it working use a TemplateColumn with a Label and Text='<%= Bind("Property") %>' in the ItemTemplate

Hope this helps
0
Josemi
Top achievements
Rank 1
answered on 17 Nov 2008, 08:57 PM
Hi,

the problem occurs when I set DataSource property from grid.

Thanks
0
Adam Nelson
Top achievements
Rank 2
answered on 17 Nov 2008, 09:02 PM
Do you have AutoGenerateColumns = true or are you using any <telerik:GridBoundColumn /> within the definition of the grid? would it be possible to paste your grid's markup?
0
Josemi
Top achievements
Rank 1
answered on 18 Nov 2008, 01:38 PM

Adam Nelson,

thanks by response.

I'm using AutoGenerateColumns = false.

The markup of the grid it's in master page and I'm creating columns in code behind ( .cs file) from .aspx page with uses this master page.

The markup of grid ( form master page):

 

<

 

telerik:RadGrid ID="Grid" runat="server" AllowSorting="True" Skin="Office2007" AllowPaging="True"

 

 

 

AllowMultiRowSelection="True" ShowGroupPanel="True" AutoGenerateColumns="False"

 

 

 

OnNeedDataSource="Grid_NeedDataSource" AllowFilteringByColumn="False" PageSize="20"

 

 

 

ShowStatusBar="True" meta:resourcekey="GridResource1" OnItemDataBound="Grid_ItemDataBound"

 

 

 

OnItemCreated="Grid_ItemCreated" OnGroupsChanging="Grid_GroupsChanging">

 

 

 

<PagerStyle Mode="NextPrevNumericAndAdvanced" />

 

 

 

<ExportSettings IgnorePaging="false" OpenInNewWindow="true">

 

 

 

<Pdf AllowAdd="false" AllowCopy="true" AllowModify="true" AllowPrinting="true" Author="Anonymous"

 

 

 

Keywords="None" FontType="Subset" PageBottomMargin="1in" PageLeftMargin="1in"

 

 

 

PageRightMargin="1in" PageTopMargin="1in" PageTitle="EPM Consist export document"

 

 

 

PaperSize="Letter" Subject="EPM Consist Export" Title="EPM Consist export" />

 

 

 

<Excel Format="Html" />

 

 

 

</ExportSettings>

 

 

 

<ClientSettings AllowDragToGroup="True" AllowColumnsReorder="True" ReorderColumnsOnClient="True">

 

 

 

<Selecting AllowRowSelect="True" />

 

 

 

<ClientEvents OnRowDblClick="RowDblClick" />

 

 

 

<ClientEvents OnRowContextMenu="RowContextMenu" />

 

 

 

<Scrolling AllowScroll="true" UseStaticHeaders="true" />

 

 

 

<Resizing AllowColumnResize="True" />

 

 

</ClientSettings>

 

 

 

<HeaderStyle Width="150px" />

 

 

 

<MasterTableView GroupLoadMode="Client" RetrieveAllDataFields="False">

 

 

 

<RowIndicatorColumn CurrentFilterFunction="NoFilter" FilterListOptions="VaryByDataType"

 

 

 

Visible="False">

 

 

 

<HeaderStyle Width="20px" />

 

 

 

</RowIndicatorColumn>

 

 

 

<ExpandCollapseColumn CurrentFilterFunction="NoFilter" FilterListOptions="VaryByDataType"

 

 

 

Resizable="False" Visible="False">

 

 

 

<HeaderStyle Width="20px" />

 

 

 

</ExpandCollapseColumn>

 

 

 

</MasterTableView>

 

 

 

</telerik:RadGrid>

 

 

 

code behind ( .cs file from .aspx page - this form uses master page above):

OBS: this code is called in Page_Load() method if not PostBack.

 

private

 

void ConfigureGrid()

 

{

 

RadGrid grid = ((MasterPages.EntityList)Page.Master).EntityGrid;

 

 

GridColumnCollection grdColumns = grid.MasterTableView.Columns;

 

 

GridClientSelectColumn selColumn = new GridClientSelectColumn();

 

grdColumns.Add(selColumn);

selColumn.ItemStyle.Width =

Unit.Pixel(40);

 

selColumn.UniqueName =

"selColumn";

 

selColumn.HeaderStyle.Width =

Unit.Pixel(40);

 

 

GridBoundColumn colId = new GridBoundColumn();

 

grdColumns.Add(colId);

colId.DataField =

"ID";

 

colId.HeaderText =

"ID";

 

colId.ReadOnly =

true;

 

colId.SortExpression =

"ID";

 

colId.UniqueName =

"ID";

 

colId.Visible =

false;

 

 

GridBoundColumn colTitle = new GridBoundColumn();

 

grdColumns.Add(colTitle);

colTitle.AllowFiltering =

true;

 

colTitle.AllowSorting =

true;

 

colTitle.Visible =

true;

 

colTitle.Groupable =

true;

 

colTitle.DataField =

"Title";

 

colTitle.HeaderText = (

string)GetLocalResourceObject("Title");

 

colTitle.SortExpression =

"Title";

 

colTitle.UniqueName =

"Title";

 

 

GridBoundColumn colType = new GridBoundColumn();

 

grdColumns.Add(colType);

colType.AllowFiltering =

true;

 

colType.AllowSorting =

true;

 

colType.Visible =

true;

 

colType.Groupable =

true;

 

 

//colType.DataField = "Type";

 

 

 

 

colType.HeaderText = (

string)GetLocalResourceObject("Type");

 

colType.SortExpression =

"Type";

 

colType.UniqueName =

"Type";

 

 

GridBoundColumn colCreator = new GridBoundColumn();

 

grdColumns.Add(colCreator);

colCreator.AllowFiltering =

true;

 

colCreator.AllowSorting =

true;

 

colCreator.Visible =

true;

 

colCreator.Groupable =

true;

 

colCreator.DataField =

"Creator.Name";

 

colCreator.HeaderText = (

string)GetLocalResourceObject("Creator");

 

colCreator.SortExpression =

"Creator.Name";

 

colCreator.UniqueName =

"Creator";

 

 

GridDateTimeColumn colCreation = new GridDateTimeColumn();

 

grdColumns.Add(colCreation);

colCreation.AllowFiltering =

true;

 

colCreation.AllowSorting =

true;

 

colCreation.Visible =

true;

 

colCreation.Groupable =

true;

 

colCreation.DataField =

"CreationDate";

 

colCreation.HeaderText = (

string)GetLocalResourceObject("CreationDate");

 

colCreation.SortExpression =

"CreationDate";

 

colCreation.UniqueName =

"CreationDate";

 

 

GridBoundColumn colModifier = new GridBoundColumn();

 

grdColumns.Add(colModifier);

colModifier.AllowFiltering =

true;

 

colModifier.AllowSorting =

true;

 

colModifier.Visible =

true;

 

colModifier.Groupable =

true;

 

colModifier.DataField =

"Modifier.Name";

 

colModifier.HeaderText = (

string)GetLocalResourceObject("Modifier");

 

colModifier.SortExpression =

"Modifier.Name";

 

colModifier.UniqueName =

"Modifier";

 

 

GridDateTimeColumn colLast = new GridDateTimeColumn();

 

grdColumns.Add(colLast);

colLast.AllowFiltering =

true;

 

colLast.AllowSorting =

true;

 

colLast.Visible =

true;

 

colLast.Groupable =

true;

 

colLast.DataField =

"LastModify";

 

colLast.HeaderText = (

string)GetLocalResourceObject("LastModify");

 

colLast.SortExpression =

"LastModify";

 

colLast.UniqueName =

"LastModify";

 

 

 

GridBoundColumn colTimeStamp = new GridBoundColumn();

 

grdColumns.Add(colTimeStamp);

colTimeStamp.Visible =

false;

 

colTimeStamp.DataField =

"TimeStampString";

 

colTimeStamp.UniqueName =

"TimeStampString";

 

}

 

Thanks,
Josemi

0
Adam Nelson
Top achievements
Rank 2
answered on 20 Nov 2008, 04:46 PM
Hmm, I tried reproducing the issue in a small sample application but was unable to get the same result, sorry I could not help more.
0
TriZetto
Top achievements
Rank 1
answered on 22 Jul 2009, 02:54 PM
I had the same problem using the Q2 2008 version of the controls.
I solved it by adding EnableLinqExpressions="false" to the grid.

Tags
Grid
Asked by
Josemi
Top achievements
Rank 1
Answers by
Adam Nelson
Top achievements
Rank 2
Josemi
Top achievements
Rank 1
TriZetto
Top achievements
Rank 1
Share this question
or