c# – 是否可以将asp:GridView绑定到List?

前端之家收集整理的这篇文章主要介绍了c# – 是否可以将asp:GridView绑定到List?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个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>

我正在尝试将DataSource设置为List< Restitution>(),其中Restitution是一个仅由公共成员组成的公共结构;即:

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;
}

数据绑定看起来也相当无害:

grdRestitutions.DataSource = restitutions;
grdRestitutions.DataBind();

但是,在DataBind()语句中抛出一个异常,其中包含以下消息:

“A field or property with the name ‘JobNumber’ was not found on the selected data source.”

我不明白为什么这不起作用;虽然大多数示例和用例似乎都使用DataSet,但它似乎应该支持实现IEnumerable的对象.为了让它能够工作,我有什么特别的事吗?

解决方法

转换公共属性的所有公共字段,它应该工作.
public struct ContainerRelease
{
    public int ContainerReleasesId {get; set;} 
    public int ContainerId {get; set;} 
    public System.DateTime ReleaseDate  {get; set;} 
    ...
}
原文链接:https://www.f2er.com/csharp/94784.html

猜你在找的C#相关文章