How To Render Thead and Tbody tag in GridView

In this post i am going to share tips and tricks about how to generate Thead and Tbody in gridview because default GridView in asp.net generate table>tr>th type of structure. uncomment this statement and see the result //Uncomment this line for THEAD and TBODY //grdPage.HeaderRow.TableSection = TableRowSection.TableHeader;

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
   <asp:GridView ID="grdPage" runat="server">
   </asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

   grdPage.DataSource = new Person().GetAll();
   grdPage.DataBind();
   //Uncomment this line for  THEAD and TBODY
   //grdPage.HeaderRow.TableSection = TableRowSection.TableHeader;
}

}
class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public List<Person> GetAll()
{

   List<Person> list = new List<Person>()
   {
       new Person{Id=1,FirstName="Barak",LastName="Obama",Age=41},
       new Person{Id=2,FirstName="Scott",LastName="Allen",Age=43},
       new Person{Id=3,FirstName="Bill",LastName="Gates",Age=21},
       new Person{Id=4,FirstName="Name",LastName="LastName",Age=21},
   };
   return list;
}

}

No comments:

Post a Comment