0

我有 2 节课:

public class clsShipper 
{
    public string ShipperName { get; set; }
}

public class clsJobInfo 
{
    public long JobID { get; set; }

    public clsShipper oShipper = new clsShipper();
}

我的 GridView 如下

<asp:GridView ID="dgvJobCostList" runat="server" CellPadding="4" ForeColor="#333333" style="font-family:Verdana;font-size:10px;" AutoGenerateColumns="False" OnSelectedIndexChanged="dgvJobCostList_SelectedIndexChanged">
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <EditRowStyle BackColor="#999999" />
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox ID="chkRowCheck" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="ID">
            <ItemTemplate>
                <asp:Label ID="lblJobID" runat="server" Text='<%# Bind("JobID") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Shipper">
            <ItemTemplate>
            <asp:Label ID="lblShipperName" runat="server" Text='<%# Bind("ShipperName") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>  

我正在尝试通过 JobInfo 类对象绑定 Shipper 类的 ShipperName 属性。我在下面试过

protected void btnSearch_Click(object sender, EventArgs e)
{
    if (oOutputJobInfo.Length < 1)
    {
        lblMessage.Text = "Error : No Data Found";
        lblMessage.Visible = true;
        return;
    }
    else
    {
        dgvJobCostList.DataSource = oOutputJobInfo;
        dgvJobCostList.DataBind();
        dgvJobCostList.Visible = true;
    }
}

但给出以下错误

DataBinding:“Nucleus.BOL.clsJobInfo”不包含名为“ShipperName”的属性。

4

1 回答 1

2

像这样导入命名空间:

<%@ Import namespace="MyNamespace.ToclsJobInfo" %>

如果与 clsJobInfo 不同,您可能需要命名空间到 clsShipper

然后您可以强制转换/取消装箱 clsJobInfo 并使用属性 oShipper

<asp:TemplateField HeaderText="Shipper">    
  <ItemTemplate>    
    <asp:Label ID="lblShipperName" runat="server" Text='<%# ((clsJobInfo)Container.DataItem).oShipper.ShipperName %>' />    
  </ItemTemplate>  
</asp:TemplateField>
于 2012-05-21T11:37:34.137 回答