Associate an ASP.NET 2.0 GridView with my own IList <T> .SomeMemberOfMyOwnCustomType.SomeProperty
Let's say I have this data:
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, for example:
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 column headers / headers:
- Friend Name (column name: friend name)
- City (column name: city)
- Age (column name: age)
How to do it?
In other words, how can I bind a GridView control to custom elements of my own custom IList selectively?
I havenβt been dealing with setforms for several years, but IIRC you can do it from the grid side using the notation <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> or use the IEnumerable / Linq extensions to convert your results like this:
GridView.DataSource = friends.Select(friend => new { FriendName, City = friend.Address.City, Age }); and create a similar <Columns> notation for this new output, you will also need this for a custom header.
EDIT: if DateField = "Address.City" will not work, there is a templateField option, and <ItemTemplate> you can just <%# Eval("Address.City") %> in your content.
Something like that:
<asp:GridView ID="FriendGridView" runat="server" AutoGenerateColumns="false"> <Columns> <asp:BoundField HeaderText="Friend Name" DataField="FriendName" SortExpression="FriendName" /> <asp:BoundField HeaderText="Age" DataField="Age" SortExpression="Age" /> <asp:BoundField HeaderText="City" DataField="Address.City" SortExpression="Address.City" /> </Columns> </asp:GridView> Check this out: ASP.NET: GridView and Business Objects
You can also create columns for your grid and map them in your code in your page_load, call the generateColumns subroutine, and then, as soon as it finishes, call the binding to it. GenerateColumns might look like this:
Private Sub GenerateContactGridColumns() Dim clmName As New BoundField() clmName.DataField = "FriendName" clmName.HeaderText = "Name" Dim clmCity As New BoundField() clmCity.DataField = "City" clmCity.HeaderText = "City" Dim clmEdit As New CommandField() clmEdit.ButtonType = ButtonType.Image clmEdit.EditImageUrl = Me.ThemeImagesPath & "/edit.gif" clmEdit.DeleteImageUrl = Me.ThemeImagesPath & "/delete.gif" clmEdit.ShowEditButton = True clmEdit.ShowDeleteButton = True gvContacts.Columns.Clear() gvContacts.Columns.Add(clmName) gvContacts.Columns.Add(clmCity) gvContacts.Columns.Add(clmEdit) End Sub In addition, I usually use the Entity infrastructure, and so an object can be added to use a partial class, and then I can define a property in an entity that is divided into sub-objects. For example, a friend might have a city property that returns address.city.