Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to bind an asp:GridView to a List<T>?

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 DataSets it does appear that it should support objects implementing IEnumerable. Is there anything special I have to do to allow it to work?

like image 543
Rowland Shaw Avatar asked Jul 07 '11 14:07

Rowland Shaw


People also ask

What is GridView DataBind () in asp net?

Use the DataBind() method to bind data from a data source to the GridView control.

What is bound field in GridView asp net?

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.

What is difference between DataGrid and GridView in ASP NET?

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.


1 Answers

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;} 
    ...
}
like image 70
Bala R Avatar answered Oct 11 '22 19:10

Bala R