Wat*_* v2 4 c# asp.net gridview
假设我有这样的数据:
class Location
{
public int Id { get; private set; }
public string City { get; set; }
public string State { get; set; }
public string Country { get; set; }
}
class Friend
{
public int Id { get; }
public string FriendName { get; set; }
public Location Address { get; set; }
public int Age { get; set; }
public bool IsReliable { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
假设我将ASP.NET 2.0 GridView控件绑定到我自己的IList,如下所示:
GridView1.DataSource = new List<Friend>
{
new Friend { Name = "...", Age = 22, ... }
};
GridView1.DataBind();
Run Code Online (Sandbox Code Playgroud)
但是我想在GridView中只使用以下列,并使用以下自定义标题/列标题:
我怎么做?
换句话说,如何有选择地将GridView控件绑定到我自己的自定义IList的自定义成员?
几年后我没有触摸webforms网格,但IIRC你可以在网格方面做到这一点,使用<Columns>符号:
<asp:GridView ...>
<Columns>
<asp:BoundField DataField="FriendName"
readonly="true"
headertext="Friend Name"/>
<asp:BoundField DataField="Address.City"
readonly="true"
headertext="City"/>
<asp:BoundField DataField="Age"
readonly="true"
headertext="Age"/>
</Columns>
</asp:GridView>
Run Code Online (Sandbox Code Playgroud)
或使用IEnumerable/Linq扩展来转换结果,如下所示:
GridView.DataSource = friends.Select(friend =>
new { FriendName, City = friend.Address.City, Age });
Run Code Online (Sandbox Code Playgroud)
并<Columns>为此新输出创建一个类似的表示法,您还需要这个用于自定义的headertext.
编辑:如果DateField ="Address.City"不起作用,则有templateField选项,<ItemTemplate>您只需<%# Eval("Address.City") %>在其内容中.
| 归档时间: |
|
| 查看次数: |
1702 次 |
| 最近记录: |