如何确定导致 Telerik RadGrid 错误的原因 System.InvalidCastException

How to determine what is causing Telerik RadGrid Error System.InvalidCastException

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

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

  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 错误,而是用户错误的转换。更改为以下内容后,一切正常。

来自

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

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

   grid.Rebind();