Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I m loading list of values in GridView control.. I have some doubt in loading values...
How to load bind "AddressName" value in BoundField inherit class...could you plz help

What I have tried:

<asp:GridView runat="server" ID="grdTask" AutoGenerateColumns="true">
<columns>
<asp:BoundField HeaderText="Name" DataField="CompanyName" />

<asp:BoundField HeaderText="Name" DataField="???????" />



public Class Company
{
public int CompanyID {get; set;}

public string CompanyName {get; set;}

public Address CompanyAddress {get; set;}

}

public Class Address
{
public int AddressID {get; set;}

public string AddressName {get; set;}

}
Posted
Updated 14-Feb-18 1:57am

1 solution

try
<asp:BoundField HeaderText="Name" DataField="CompanyAddress.AddressName" />

or
<%#DataBinder.Eval(Container.DataItem, "CompanyAddress.AddressName")%>
 
Share this answer
 
v2
Comments
venkatesh (chennai) 14-Feb-18 8:13am    
I have tried the above one ji .. but getting error(A field or property with the name 'CompanyAddress.AddressName' was not found on the selected data source.)
Karthik_Mahalingam 14-Feb-18 8:15am    
show your datasource,

i have tried like this
   List<company> lst = new List<company>();
            Address add = new Address () { AddressID = 1, AddressName = "aa"};
            lst.Add( new Company (){ CompanyAddress = add , CompanyID = 122, CompanyName = "Code  "});
            lst.Add(new Company() { CompanyAddress = add, CompanyID = 222, CompanyName = "  Project" });
            grdTask.DataSource = lst;
            grdTask.DataBind();
venkatesh (chennai) 14-Feb-18 8:24am    
yes ji...my datasource like the above one...
and I have tried TemplateField
like::: Text='<%# Eval("CompanyAddress.AddressName") %>'
this.. now working ji
venkatesh (chennai) 14-Feb-18 8:25am    
now working fine... thank for Quick reply..
Karthik_Mahalingam 14-Feb-18 8:27am    
welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900