0
protected void passSubContractorInfoToNewPage(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Sub_Contractor subC = (Sub_Contractor)e.Row.DataItem;
            HyperLink LoadSubContractorDetails = (HyperLink)e.Row.FindControl("LoadSubContractorDetails");
            LoadSubContractorDetails.NavigateUrl = ResolveUrl(@"~/SubContractDetails.aspx?id=" + subC.id.ToString());
        }
}
<asp:GridView ID="GridView2" OnRowDataBound="passSubContractorInfoToNewPage" runat="server" AllowPaging="True" AllowSorting="True" CssClass="gridview" AutoGenerateColumns="false" DataSourceID="GridDataSource1"> 
        <Columns>
        <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="InvoiceInfoID" /> 
        <asp:BoundField DataField="Company Name" HeaderText="Company Name" SortExpression="Name" />   
        <asp:TemplateField>
            <ItemTemplate>
                <asp:HyperLink ID="LoadSubContractorDetails" runat="server" Text="Show Details"/>
             </ItemTemplate>
        </asp:TemplateField>



   </asp:GridView>
     <asp:SqlDataSource ID="GridDataSource1" runat="server"   
          ConnectionString="<%$ConnectionStrings:ClarkesTest4FromMaster1ConnectionString %>"  
         SelectCommand="SELECT id, [Company Name] FROM [Sub Contractor] ORDER BY [Company Name]" >
    </asp:SqlDataSource> 

这将返回 subContractor 为 NULL 的错误,错误为:

Unable to cast object of type 'System.Data.DataRowView' to type 'Sub_Contractor'.

也试过

  if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView view = (DataRowView)e.Row.DataItem;

但仍然没有喜悦......

为什么它不接分包商有人可以告诉我吗?

谢谢

4

1 回答 1

2

无法将“System.Data.DataRowView”类型的对象转换为“Sub_Contractor”类型。

这是不言自明的。所以每个的底层数据源GridViewRow是一个DataRowView.

但我不明白这一点:

也试过

if (e.Row.RowType == DataControlRowType.DataRow)
{
    DataRowView view = (DataRowView)e.Row.DataItem;

但仍然没有喜悦......

这应该没有问题。所以我认为这会导致您尚未提到的不同错误。

于 2013-02-18T13:43:11.430 回答