Let’s say I have data like such:
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; }
}
Let’s say I bind an ASP.NET 2.0 GridView control to my own IList like so:
GridView1.DataSource = new List<Friend>
{
new Friend { Name = "...", Age = 22, ... }
};
GridView1.DataBind();
But I want to have only the following columns in my GridView with the following custom captions/column headers:
- FriendName (Column Caption: Friend Name)
- City (Column Caption: City)
- Age (Column Caption: Age)
How do I do that?
In other words, how do I bind a GridView control to a custom members of my own custom IList selectively?
Been a few years I don’t touch webforms grids but IIRC you can either do that on the grid side, using the
<Columns>notation:or use the IEnumerable/Linq extensions to transform your resuls as so:
and create a similar
<Columns>notation for this new output, you will also need this for the customized headertext.EDIT: In case the DateField=”Address.City” won’t work, there is the templateField option, with the
<ItemTemplate>you can simply<%# Eval("Address.City") %>in its content.