gpt4 book ai didi

c# - 是否可以绑定(bind)一个asp :GridView to a List?

转载 作者:太空狗 更新时间:2023-10-29 21:02:48 25 4
gpt4 key购买 nike

我有一个 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)成员组成的公共(public)结构;即:

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

数据绑定(bind)看起来也很无害:

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)字段转换为公共(public)属性,它应该可以工作。

public struct ContainerRelease
{
public int ContainerReleasesId {get; set;}
public int ContainerId {get; set;}
public System.DateTime ReleaseDate {get; set;}
...
}

关于c# - 是否可以绑定(bind)一个asp :GridView to a List<T>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6612492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com