用 IEnumerable 对象填充 GridView

问题描述 投票:0回答:1

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

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 IEnumerable -->
  </tbody>
</table>

我可以使用

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

asp.net webforms
1个回答
5
投票

您可以使用带有显式

HeaderText
绑定字段的绑定字段。
使用自动生成列为 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

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

© www.soinside.com 2019 - 2024. All rights reserved.