Is it possible to associate asp: GridView with List <T>?
I have a gridview:
<asp:GridView ID="grdRestitutions" runat="server" AutoGenerateColumns="False"> <Columns> <asp:BoundField DataField="JobNumber" HeaderText="Job" /> <asp:BoundField DataField="ContainerType" HeaderText="Type" /> <asp:BoundField DataField="ReleaseDate" HeaderText="Date" /> <asp:BoundField DataField="Commodity" HeaderText="Commodity" /> <asp:BoundField DataField="GrossWeight" HeaderText="Weight" /> <asp:BoundField DataField="SpecialInstructions" HeaderText="Special Instructions" /> </Columns> </asp:GridView> That I am trying to set the DataSource as List<Restitution>() , where Restitution is a public structure consisting only of public members; i.e:.
public struct Restitution { public int ContainerReleasesId; public int ContainerId; public System.DateTime ReleaseDate; public int DepotId; public string DepotName; public string JobNumber; public string BillOfLadingNumber; public string BookingType; public string Commodity; public string SpecialInstructions; public int GrossWeight; public bool Confirmed; public bool RecievedFlag; public bool ReleaseSource; public int ContainerTypeId; public string InOut; public string ContainerTypeDescription; } Data binding looks pretty harmless:
grdRestitutions.DataSource = restitutions; grdRestitutions.DataBind(); However, an exception is thrown in the DataBind() statement with a less useful message:
"A field or property named" JobNumber "was not found in the selected data source."
I do not understand why this does not work; while most examples and use cases seem to use a DataSet , it looks like it should support objects that implement IEnumerable . Is there anything special I have to do so that it can work?
Try the following:
<asp:GridView ID="grdRestitutions" runat="server" AutoGenerateColumns="False"> <Columns> <asp:templatefield headertext="Job"> <itemtemplate> <asp:label id="JobNumberLabel" Text="<%# ((Restitution)Container.DataItem).JobNumber %>" runat="server"/> </itemtemplate> </asp:templatefield> </Columns> </asp:GridView> This code passes each related line to the Restitution object, then accesses the JobNumber field JobNumber . If this works, you can link the other fields in the same way. If this does not work, the error will lead you to a real problem.
Yes, you should be able to bind the list in this case. The fact is that you need to do something more:
<asp:GridView ID="grdRestitutions" runat="server" AutoGenerateColumns="False"> <Columns> <asp:BoundField DataField="DataItem.JobNumber" HeaderText="Job" /> <asp:BoundField DataField="DataItem.ContainerType" HeaderText="Type" /> <asp:BoundField DataField="DataItem.ReleaseDate" HeaderText="Date" /> <asp:BoundField DataField="DataItem.Commodity" HeaderText="Commodity" /> <asp:BoundField DataField="DataItem.GrossWeight" HeaderText="Weight" /> <asp:BoundField DataField="DataItem.SpecialInstructions" HeaderText="Special Instructions" /> </Columns> </asp:GridView> If this does not work, you can try using TemplateColumns like this:
<asp:GridView ID="grdRestitutions" runat="server" AutoGenerateColumns="False"> <Columns> <asp:TemplateField HeaderText="Job" > <ItemTemplate> <%# DataBinder.Eval(Container, "DataItem.Job") %> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> Not tested, but should give you somewhere ...