I've got 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'm trying to set the DataSource to be a 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;
}
The data binding looks fairly innocuous, too:
grdRestitutions.DataSource = restitutions;
grdRestitutions.DataBind();
However, an exception is thrown on the DataBind()
statement with the less than helpful message of:
"A field or property with the name 'JobNumber' was not found on the selected data source."
I don't understand why this isn't working; whilst most examples and use cases seem to use DataSet
s it does appear that it should support objects implementing IEnumerable
. Is there anything special I have to do to allow it to work?
Use the DataBind() method to bind data from a data source to the GridView control.
The BoundField class is used by data-bound controls (such as GridView and DetailsView) to display the value of a field as text. The BoundField object is displayed differently depending on the data-bound control in which it is used.
The DataGrid and the GridView controls have different event models. The DataGrid control raises single events for operations, while the GridView control is capable of both pre-operation and post-operation events. The GridView control supports the Sorting event that occurs when a field is sorted.
Convert all public fields for public properties and it should work.
public struct ContainerRelease
{
public int ContainerReleasesId {get; set;}
public int ContainerId {get; set;}
public System.DateTime ReleaseDate {get; set;}
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With