gpt4 book ai didi

asp.net - 用 IENumerable 对象填充 GridView

转载 作者:行者123 更新时间:2023-12-04 15:13:07 25 4
gpt4 key购买 nike

我有一个看起来像这样的对象:

public class TestData
{
public string FirstName {get; set;}
public string LastName {get; set;}
public string Email {get; set;}
public string Phone {get; set;}
}

我有 10 个该类的实例存储在 IENumerable 中.

我还有一个 GridView在我的 aspx文件:
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>

我需要的是一种显示 IENumerable 内容的方法在 GridView .但我希望能够在 thead 中设置“标题”我自己的表。

所以我得到这样的东西:
<table>
<thead>
<th>Firstname</th>
<th>Firstname</th>
<th>Telephone</th>
<th>Email address</th>
</thead>
<tbody>
<!-- the values from each TestData class stored in the IENumberable -->
</tbody>
</table>

我可以用 GridView 来做到这一点吗?还是使用其他控件更好地完成这项工作?我还记得一些关于模板的事情?不确定,我对 ASP.NET 很陌生。

最佳答案

您可以使用带有显式 HeaderText 的绑定(bind)字段的绑定(bind)字段。
使用自动生成列为假。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumn="false">
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName " HeaderText="Last Name" />
.....
</asp:GridView>

编辑 1
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumn="false">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName " HeaderText="Last Name" />
.....
</Columns>
</asp:GridView>

关于asp.net - 用 IENumerable 对象填充 GridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14997393/

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