gridview数据源中缺少字段?

时间:2011-06-12 17:57:37

标签: c# asp.net sql-server visual-studio oop

我正在加载一个带有对象列表的gridview,而我在aspx页面上调用一些嵌套属性时遇到了麻烦。我知道列表包含它们,并且可以返回它们,因为我使用了print方法来调用有问题的几个属性。细节:提交对象包含嵌套对象Cust,Bro和SubCoverage,每个对象都有自己的属性,我将其存储在列表中并尝试调用gridview。 Submission对象本身的属性显示正常,但是当我调用Cust对象的CustName1属性时,它是“在所选数据源上找不到字段”。因为我知道数据是存在的,并且可以通过代码隐藏中的其他方法调用,所以我假设它是一个简单的语法,但我已经尝试了足够的迭代来让我想知道是否有一些我缺少的东西。它是什么?代码如下:

//提交类/对象:

public class Submission 
{

    private int SubmissionId;

    public int SubmissionId1
    {
        get { return SubmissionId; }
        set { SubmissionId = value; }
    }

    private string Status;

    public string Status1
    {
        get { return Status; }
        set { Status = value; }
    }

    private string StatusComment;

    public string StatusComment1
    {
        get { return StatusComment; }
        set { StatusComment = value; }
    }


    private List<Product> Products = new List<Product>();

    public List<Product> Products1
    {
        get { return Products; }
        set { Products = value; }
    }

    private Customer Cust;
    public Customer _Cust
    {
        get { return Cust; }
        set { Cust = value; }
    }
    private Broker Bro;
    public Broker _Bro
    {
        get { return Bro; }
        set { Bro = value; }
    }


    private SubmissionCoverage Subcov;

    public SubmissionCoverage _SubCov
    {
        get { return Subcov; }
        set { Subcov = value; }
    }

    public Submission()
    {
        Cust = new Customer();
        Bro = new Broker();
        Subcov = new SubmissionCoverage();
    }
}

//其中一个嵌套对象类Customer:

public class Customer 

{

private int CustId;

public int CustId1
{
    get { return CustId; }
    set { CustId = value; }
}


private string CustName;

public string CustName1
{
    get { return CustName; }
    set { CustName = value; }
}


private string CustAddress;

public string CustAddress1
{
    get { return CustAddress; }
    set { CustAddress = value; }
}


private string CustState;

public string CustState1
{
    get { return CustState; }
    set { CustState = value; }
}

private string CustCity;

public string CustCity1
{
    get { return CustCity; }
    set { CustCity = value; }
}


private int CustZip;

public int CustZip1
{
    get { return CustZip; }
    set { CustZip = value; }
}

private int SicNaic;

public int SicNaic1
{
    get { return SicNaic; }
    set { SicNaic = value; }
}

}

//查询/列表数据层:

Submission tempSubmission = new Submission();
        while (dr.Read())
        {
            tempSubmission = new Submission();
            tempSubmission.SubmissionId1 = dr.GetInt32(0);
            tempSubmission._Cust.CustName1 = dr.GetString(1);
            tempSubmission._Cust.CustCity1 = dr.GetString(2);
            tempSubmission._Cust.CustState1 = dr.GetString(3);
            tempSubmission._Bro.BroName1 = dr.GetString(4);
            tempSubmission._Bro.BroState1 = dr.GetString(5);
            tempSubmission._Bro.Entity1 = dr.GetString(6);
            tempSubmission._SubCov.Coverage1 = dr.GetInt32(7);
            tempSubmission.Status1 = dr.GetString(8);

            lstSubmission.Add(tempSubmission);

        }
        return lstSubmission;
    }   

}

// gridview代码:

 <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" EmptyDataText="There are no data records to display." AllowPaging="True" 
    BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" 
    CellPadding="3" GridLines="Vertical" HorizontalAlign="Center" 
    AllowSorting="True" >
    <AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>

        <asp:BoundField DataField="SubmissionId1" HeaderText="Submission Id" 
            SortExpression="SubmissionId1" >
        </asp:BoundField>
        <asp:BoundField DataField="_Cust.CustName1" HeaderText="Customer" 
            SortExpression="tempsub._Cust.CustName1" />
        <asp:BoundField DataField="CustCity1" HeaderText="Customer City" 
            SortExpression="CustCity1" />
        <asp:BoundField DataField="CustState1" HeaderText="Customer State" 
            SortExpression="CustState1" />
        <asp:BoundField DataField="BroName1" HeaderText="Broker" 
            SortExpression="BroName1" />
        <asp:BoundField DataField="BroState1" HeaderText="Broker State" 
            SortExpression="BroState1" />
        <asp:BoundField DataField="Entity1" HeaderText="Entity Type" 
            SortExpression="Entity1" />
        <asp:BoundField DataField="Coverage" 
            HeaderText="Coverage" SortExpression="Coverage" 
            DataFormatString=" {0:c}" />
        <asp:BoundField DataField="Status1" HeaderText="Status" 
            SortExpression="Status1" />

        <asp:HyperLinkField DataNavigateUrlFields="SubmissionId" 
            DataNavigateUrlFormatString="View.aspx?SubmissionId={0}" Text="View" 
            HeaderText="View" />
        <asp:HyperLinkField DataNavigateUrlFields="SubmissionId" 
            DataNavigateUrlFormatString="ViewEdit.aspx?SubmissionId={0}" HeaderText="Edit" 
            Text="Edit" />

    </Columns>

</asp:GridView>

//最后,表示层中的print方法证明我并不疯狂:

 SubmissionService ss = new SubmissionService();
    List<Submission> SubmissionsList = ss.getAllSubmission();


    GridView2.DataSource = SubmissionsList;
    GridView2.DataBind();

    Submission tempsub = SubmissionsList.First();
    Response.Write("Printing temp:" + tempsub._Cust.CustName1 + " total count is:" + SubmissionsList.Count() );  // *This returns the correct data for whichever field I specify.*

1 个答案:

答案 0 :(得分:1)

试试看:

http://www.dailycode.info/Blog/post/2009/01/11/ASPNet-GridView-databinding-a-subobject.aspx

这篇文章的关键是使用带有标签的TemplateField:

<asp:TemplateField HeaderText="Stock location description">
 <ItemTemplate>
   <asp:Label ID="lblCRCID" runat="server"
    Text='<%# ((Namespace.TestStock)(Container.DataItem)).StockLocation.Description %>'>
   </asp:Label>
  </ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Description" HeaderText="Stock descrioption" />

HTH