Field missing from gridview datasource?
I'm loading a gridview with a list of objects and I'm having trouble calling some of the nested properties on the aspx page. I know the list contains them, and they can be returned, because I've used a print method to call up several of the properties in question. The specifics: Submission objects contain nested objects Cust, Bro, and SubCoverage, each of which have their own properties I'm storing in the list and trying to call up for the gridview. Properties of the Submission object itself show up fine, but when I call up the CustName1 property of the Cust object, it's "Field not found on the selected datasource". Since I know the data is there, and callable by other methods in the codebehind, I'm assuming it's a simple syntax thing, but I've tried enough iterations to make me wonder if there's something I'm missing. What is it? Code below:
// The submission class/object:
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();
}
}
//One of the nested object classes, 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; }
}
}
//The query/list data layer:
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;
}
}
// The gridview code:
<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" SortExp开发者_JS百科ression="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>
//And finally, the print method in the presentation layer that proves I'm not crazy:
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.*
Try look here:
http://www.dailycode.info/Blog/post/2009/01/11/ASPNet-GridView-databinding-a-subobject.aspx
The key in this post is to use a TemplateField with a label:
<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
精彩评论