Asp.net 用IENumerable对象填充GridView
我有一个像这样的对象:Asp.net 用IENumerable对象填充GridView,asp.net,webforms,Asp.net,Webforms,我有一个像这样的对象: public class TestData { public string FirstName {get; set;} public string LastName {get; set;} public string Email {get; set;} public string Phone {get; set;} } <table> <thead> <th>Firstname</th
public class TestData
{
public string FirstName {get; set;}
public string LastName {get; set;}
public string Email {get; set;}
public string Phone {get; set;}
}
<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>
我在IENumerable
中存储了该类的10个实例
我的aspx
文件中还有一个GridView
:
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
我可以用
GridView
来完成这项工作吗?还是使用其他控件来完成这项工作更好?我还记得一些关于模板制作的事情?不确定,我对ASP.NET很陌生。您可以将绑定字段与绑定字段的显式标题文本一起使用。
使用“自动生成列”设置为false
<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
.....
谢谢,我理解边界字段
,但我仍然不确定如何将我的IENumerable
绑定到gridview。我所能找到的只是关于如何将其连接到数据库的信息。知道如何将我的IENumerable
绑定到它吗?我还遇到了一个错误:GridView没有名为“BoundField”的公共属性@Vivendi很抱歉,它应该在列标记内。我已经编辑了我的答案。谢谢,现在可以了。关于“将对象绑定到网格”,我是这样做的(对于其他需要它的人):我创建了一个列表
,其中包含我的TestData
类的一个实例。我将列表添加到GridView中,如下所示:GridView1.DataSource=myList;GridView1.DataBind()代码>---现在一切正常:)
<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>