如何确定导致Telerik RadGrid错误System.InvalidCastException的原因
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何确定导致Telerik RadGrid错误System.InvalidCastException的原因相关的知识,希望对你有一定的参考价值。
在尝试以编程方式绑定(重新绑定)RadGrid时,InvalidCastException
被抛出并查看消息,它只声明Additional information: Specified cast is not valid. occurred
。网格包含在RadMultiPage中的用户控件中。网格的数据源绑定在选项卡上。以下代码获取usercontrol并设置数据源(成功检索数据)。
如何判断导致异常的绑定?
protected void OrderItemTabStrip_TabClick(object sender, RadTabStripEventArgs e)
{
if (e.Tab.Value == @"ValueHistory")
{
var controls = e.Tab.PageView.Controls;
OrderHistoryAndVariance control;
foreach (Control parent in controls)
{
if (parent.ID == "ValueHistory")
{
foreach (Control child in parent.Controls)
{
if (child.ID == "OrderHistoryAndVariancecGrid")
{
((RadGrid) child).DataSource = GetVarianceHistory();
try
{
((RadGrid)child).Rebind();
}
catch (Exception exception)
{
//exception message doesn't indicate what failed
throw;
}
}
break;
}
}
}
}
e.Tab.PageView.Selected = true;
e.Tab.Selected = true;
e.Tab.Focus();
}
网格:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="OrderHistoryAndVariance.ascx.cs" Inherits="DAME.UserInterface.App.OrderHistoryAndVariance" %>
<telerik:radgrid id="OrderHistoryAndVariancecGrid" runat="server" allowsorting="False" gridlines="None" onitemdatabound="RadGrid1_ItemDataBound" style="font-weight: normal; font-size: 10pt; font-family: Calibri, Verdana, Arial; min-height: 80px;"
autogeneratecolumns="False" groupingenabled="False" DataMember="ValueHistoryViewModel">
<MasterTableView ClientDataKeyNames="OrderId,OrderItemId" TableLayout="Fixed">
<Columns>
<telerik:GridTemplateColumn DataField="OrderId" HeaderText="ID" UniqueName="HyperLink">
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Font-Size="Smaller" Width="100px" />
<ItemStyle HorizontalAlign="Center" Font-Size="Smaller" Width="100px" />
<ItemTemplate>
<asp:HyperLink ID="HyperLink11" Target="_new" Font-Bold="true" ForeColor="#FF6801" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "OrderId") + "." + DataBinder.Eval(Container.DataItem, "OrderItemId") %>'
NavigateUrl='<%# "~/App/OrderView.aspx?OrderId=" + DataBinder.Eval(Container.DataItem, "OrderId") +"&OrderItemId="+DataBinder.Eval(Container.DataItem, "OrderItemId") %>'>HyperLink</asp:HyperLink>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="VendorID" UniqueName="VendorId">
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Font-Size="Smaller" Width="100px" />
<ItemStyle HorizontalAlign="Center" Font-Size="Smaller" Width="100px" />
<ItemTemplate>
<asp:Label ID="lblSameVednor" runat="server" Text='<%# (((int)Eval("VendorId")) == 0 ? "" : Eval("VendorId")) %>' Font-Bold="true" Font-Size="10px" ForeColor="#FF6801"
Visible='<%# IsSameVendor((int)Eval("VendorId")) %>'></asp:Label>
<asp:Label ID="lblDifferentVendor" runat="server" Text='<%# Eval("VendorId") %>' Font-Size="10px"
Visible='<%# !IsSameVendor((int)Eval("VendorId")) %>'></asp:Label>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn DataField="ClientName" HeaderText="Client">
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Font-Size="Smaller" Width="150px" />
<ItemStyle HorizontalAlign="Center" Font-Size="Smaller" Width="150px" />
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="ProductName" HeaderText="Product">
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Font-Size="Smaller" Width="150px" />
<ItemStyle HorizontalAlign="Center" Font-Size="Smaller" Width="150px" />
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="InspectionType" HeaderText="Inspection Type">
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Font-Size="Smaller" Width="80px" />
<ItemStyle HorizontalAlign="Center" Font-Size="Smaller" Width="80px" />
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn HeaderText="Value" UniqueName="AppraisedValue">
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Font-Size="Smaller" Width="80px" />
<ItemStyle HorizontalAlign="Center" Font-Size="Smaller" Width="80px" />
<ItemTemplate>
<asp:Label ID="lblValue" runat="server" Text='<%# Eval("AppraisedValue") %>' Font-Size="Smaller"
Visible='<%# IsValueGreaterThanZero((Double)Eval("AppraisedValue")) %>'></asp:Label>
<asp:HyperLink ID="hypValue" runat="server" Text='<%# Eval("AppraisedValue") %>' Font-Size="Smaller" Font-Bold="true" Target="_blank" ForeColor="#FF6801"
NavigateUrl='<%# "~/App/ViewDocument.aspx?DocumentID=" + DataBinder.Eval(Container.DataItem, "DocumentId") %>'
onclick="javascript:w=window.open(this.href,'docpopup','width=700,height=700,resizable=yes,scrollbars=yes,toolbar=no,location=no,status=no,menubar=no,copyhistory=no');return false;"
Visible='<%# !(IsValueGreaterThanZero((Double)Eval("AppraisedValue"))) %>'></asp:HyperLink>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn DataField="VarianceValue" HeaderText="Variance" UniqueName="VarianceValue">
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Font-Size="Smaller" Width="70px" />
<ItemStyle HorizontalAlign="Center" Font-Size="Smaller" Width="70px" />
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="VariancePercent" HeaderText="% Var">
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Font-Size="Smaller" Width="70px" />
<ItemStyle HorizontalAlign="Center" Font-Size="Smaller" Width="70px" />
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="PropertyType" HeaderText="Property Type">
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Font-Size="Smaller" Width="100px" />
<ItemStyle HorizontalAlign="Center" Font-Size="Smaller" Width="100px" />
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="GLA" HeaderText="GLA">
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Font-Size="Smaller" Width="70px" />
<ItemStyle HorizontalAlign="Center" Font-Size="Smaller" Width="70px" />
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Bedrooms" HeaderText="Bed">
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Font-Size="Smaller" Width="50px" />
<ItemStyle HorizontalAlign="Center" Font-Size="Smaller" Width="50px" />
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Bathrooms" HeaderText="Bath">
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Font-Size="Smaller" Width="50px" />
<ItemStyle HorizontalAlign="Center" Font-Size="Smaller" Width="50px" />
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="PropertyCondition" HeaderText="Condition">
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Font-Size="Smaller" Width="100px" />
<ItemStyle HorizontalAlign="Center" Font-Size="Smaller" Width="100px" />
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="AVMValueVariance" HeaderText="AVM Variance">
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Font-Size="Smaller" Width="80px" />
<ItemStyle HorizontalAlign="Center" Font-Size="Smaller" Width="80px" />
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="ConfidenceScore" HeaderText="Confidence Score">
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Font-Size="Smaller" Width="80px" />
<ItemStyle HorizontalAlign="Center" Font-Size="Smaller" Width="80px" />
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="RiskLevel" HeaderText="Risk Level">
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Font-Size="Smaller" Width="80px" />
<ItemStyle HorizontalAlign="Center" Font-Size="Smaller" Width="80px" />
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="PropertyClassification" HeaderText="Property Classification">
<HeaderStyle HorizontalAlign="Center" Font-Bold="true" Font-Size="Smaller" Width="80px" />
<ItemStyle HorizontalAlign="Center" Font-Size="Smaller" Width="80px" />
</telerik:GridBoundColumn>
</Columns>
</MasterTableView>
<ClientSettings AllowKeyboardNavigation="false" EnableRowHoverStyle="True">
<Scrolling AllowScroll="True" UseStaticHeaders="True"/>
<Selecting AllowRowSelect="True" EnableDragToSelectRows="False" />
<ClientEvents OnKeyPress="OnKeyPress" />
</ClientSettings>
<PagerStyle Mode="NextPrevAndNumeric" Position="Bottom" PageSizeControlType="None" AlwaysVisible="true" />
<GroupingSettings CaseSensitive="false" />
</telerik:radgrid>
用于数据源的视图模型(它是一个列表):
public class ValueHistoryViewModel
{
public int OrderId { get; set; }
public int OrderItemId { get; set; }
public DateTime? Date { get; set; }
public decimal VariancePercent { get; set; } = 0M;
public decimal VarianceValue { get; set; } = 0M;
public bool IsCompletedOrder { get; set; }
public bool VarianceApproved { get; set; }
public decimal AppraisedValue { get; set; } = 0M;
//VarianceHistoryData
public string ProductName { get; set; }
public string InspectionType { get; set; }
public int VendorId { get; set; }
public string ClientName { get; set; }
public int? GLA { get; set; }
public int Bedrooms { get; set; }
public decimal Bathrooms { get; set; } = 0M;
public string PropertyCondition { get; set; }
public string YearBuilt { get; set; }
public int? DocumentId { get; set; }
public string PropertyClassification { get; set; }
public decimal CAValue { get; set; } = 0M;
public string RiskLevel { get; set; }
public double ConfidenceScore { get; set; } = 0.00D;
public decimal AVMValueVariance { get; set; } = 0M;
public string PropertyType { get; set; }
}
答案
结束,这不是一个telerik错误,它是用户糟糕的铸造。更改为以下后,所有都按预期工作。
FROM
((RadGrid)child).DataSource = GetVarianceHistory();
((RadGrid)child).Rebind();
TO
var grid = (RadGrid)child;
grid.DataSource = GetVarianceHistory();
grid.Rebind();
以上是关于如何确定导致Telerik RadGrid错误System.InvalidCastException的原因的主要内容,如果未能解决你的问题,请参考以下文章