加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 06:44:56 所属栏目:百科 来源:网络整理
导读:我有一个GridView: asp:GridView ID="grdRestitutions" runat="server" AutoGenerateColumns="False" Columns asp:BoundField DataField="JobNumber" HeaderText="Job" / asp:BoundField DataField="ContainerType" HeaderText="Type" / asp:BoundField Dat
我有一个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;} 
    ...
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读