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
To get it working use a TemplateColumn with a Label and Text='<%= Bind("Property") %>' in the ItemTemplate
Hope this helps
Thanks
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
I solved it by adding EnableLinqExpressions="false" to the grid.