Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill GridView with IENumerable objects

I have an object which 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 that class stored in a IENumerable.

I also have a GridView in my aspx file:

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

What i need is a way to display the contents of the IENumerable in the GridView. But i want to be able to set the "titles" in the thead of the table myself.

So that 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? I also remember something about Templating? Not sure, i'm pretty new to ASP.NET.

like image 234
Vivendi Avatar asked Feb 21 '13 08:02

Vivendi


1 Answers

You can use bound field with explicit HeaderText of the bound field.
Use auto generate columns to 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>

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

like image 72
शेखर Avatar answered Sep 20 '22 14:09

शेखर