views:

161

answers:

1

I'm creating an anonymous List<> here:

var pip = new { MCP = "", Measure = "", Year = "", url1 = "", url2 = "", url3 = "" };
var PipList = (new[] { pip }).ToList();

The I loop through my code and load that list with items and bind it to my gridview:

PipList.RemoveAt(0);
gvReport.DataSource = PipList;
gvReport.DataBind();

When I debug this I see that the List<> has items in it right before I bind it, but when I view the gridview after the bind it's empty. Is it not possible to do this?

I've also tried defining a class and not using an anonymous object and it doesn't work either.

If it helps this is the gridview

<asp:GridView ID="gvReport" Width="750" AutoGenerateColumns="false" runat="server"
                            AllowSorting="false" AllowPaging="false" CellPadding="4" GridLines="Both"
                            CssClass="gv_Style" Visible="false">
                            <HeaderStyle BackColor="#000000" ForeColor="White" />
                            <AlternatingRowStyle CssClass="gv_AlternatingRow" />
                            <RowStyle CssClass="gv_Row" />
                            <PagerStyle CssClass="gv_Pager" />
                            <Columns>
                                <asp:BoundField HeaderStyle-HorizontalAlign="Center" DataField="MCP" HeaderText="MCP" />
                                <asp:BoundField HeaderStyle-HorizontalAlign="Center" HeaderText="PIP Measure" DataField="Measure" />
                                <asp:BoundField HeaderStyle-HorizontalAlign="Center" HeaderText="Start Year" DataField="Year"  />
                                <asp:BoundField HeaderStyle-HorizontalAlign="Center" HeaderText="Summary" DataField="url1" htmlencode="false" />
                                <asp:BoundField HeaderStyle-HorizontalAlign="Center" HeaderText="Detail" DataField="url2" htmlencode="false" />
                                <asp:BoundField HeaderStyle-HorizontalAlign="Center" DataField="url3" htmlencode="false" HeaderText="Yearly Summary"  />
                            </Columns>
                        </asp:GridView>
A: 

Hi Jhorra,

You can actually bind an anonymous or defined class list to a GridView. From your code, there is possibly two reasons why it isn't working.

  1. You created a list of anonyomous objects with one item in it and then in your next code snip, you removed it with PipList.RemoveAt(0); thus leaving your list empty.
  2. If you added other items after the PipList.RemoveAt(0); statement, did you set the GridView's Visible property to true. In your code, it is set to false meaning it isn't visible.

Hope that helps.

Matrich

Matrich