Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I would be grateful if you could help me to solve the problem. Im trying to display a nested anonymous type in a GridView control. But the GridView control displaying only OrderID and OrderDate fields (i.e. it doesn't display nested type).
I'm using the following code:

C#
protected void btn2_Click(object sender, EventArgs e)
                   {
                      var query = (from o in ctx2.Orders 
                                      select new 
                                      {o.OrderID, o.OrderDate,
                                         nest_anon = new {          
                                             o.Customer,
                                             o.ShipAddress,
                                              o.Status }});
 tb2.Text = (query as ObjectQuery).ToTraceString(); 
 gv2.DataSource=query;
 gv2.DataBind();


What should i fix in my code to display anonymous and nested anonymous type in my GridView? Thank you in advance.
Posted
Updated 1-Dec-11 15:17pm
v3
Comments
Sergey Alexandrovich Kryukov 30-Nov-11 13:56pm    
It's more or less clear except "display a type". What do you mean by that? Type name, fully-qualified type name, some reflected type meta-data, or perhaps some representation of the instance of the type (ToString? :-)? What's the purpose? What's your goal?
--SA
[no name] 1-Dec-11 21:17pm    
EDIT: added "code" tag

ok, now I get what you mean.

All you have to do is set the AutoGenerateColumns property of the gridview to false. Then apply a column templates. You need to edit your aspx on this one.
ASP.NET
<asp:GridView ID="grdMain" runat="server" AutoGenerateColumns="False">
  <columns>
    <asp:BoundField DataField="OrderID" HeaderText="OrderID" />
    <asp:BoundField DataField="OrderDate" HeaderText="OrderDate" />
    <asp:BoundField DataField="nest_anon" HeaderText="Nested Type" />
  </columns>
</asp:GridView>
 
Share this answer
 
v5
Comments
Mich_90 2-Dec-11 19:26pm    
But yet,a cant display nested type.As i undertood there is no trivial solution for this task.
I'm not really sure what you're trying to do, have you tried:
C#
gv2.DataSource = query.ToList();
 
Share this answer
 
v2
Comments
Mich_90 1-Dec-11 12:15pm    
Yes Michael i tried to do this: gv2.DataSource = query.ToList();

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