如何确定是什么原因导致Telerik RadGrid错误System.InvalidCastException

时间:2019-04-30 18:47:33

标签: asp.net telerik radgrid

在尝试以编程方式绑定(重新绑定)RadGrid时,会抛出InvalidCastException,并查看仅声明为Additional information: Specified cast is not valid. occurred的消息。网格包含在RadMultiPage中的用户控件内。单击选项卡时,将绑定网格的数据源。以下代码获取用户控件并设置数据源(数据已成功检索)。

如何确定是哪个绑定导致了异常?

  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; }
      }      

1 个答案:

答案 0 :(得分:0)

最后,这不是Telerik错误,而是用户错误的转换。更改为以下内容后,所有功能均按预期工作。

FROM

  ((RadGrid)child).DataSource = GetVarianceHistory();
  ((RadGrid)child).Rebind();

TO

   var grid = (RadGrid)child;
   grid.DataSource = GetVarianceHistory();

   grid.Rebind();