微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.jb51.cc/csharp/94784.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐