Populate GridView with IENumerable

I have an object that looks like this:

public class TestData { public string FirstName {get; set;} public string LastName {get; set;} public string Email {get; set;} public string Phone {get; set;} } 

I have 10 instances of this class stored in IENumerable .

I also have a GridView in my aspx file:

 <asp:GridView ID="GridView1" runat="server"> </asp:GridView> 

I need a way to display IENumerable content in a GridView . But I want to be able to set "headers" in thead tables.

So, I get something like this:

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

Can I do this with a GridView or is it better to use some other control for the job? Do I also remember something about Templating? Not sure, I'm pretty new to ASP.NET.

+6
source share
1 answer

You can use the associated field with an explicit HeaderText associated field.
Use auto generate columns for 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> 

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

+5
source

All Articles