SlideShare a Scribd company logo
1 of 6
Download to read offline
9/1/2015 Programmatically creating GridView header row in ASP.NET using C#
http://www.dotnetfox.com/articles/programmatically­creating­gridview­header­row­in­Asp­Net­using­C­Sharp­1059.aspx 1/6
by: Saravanan Arumugam Date: 25/10/2013 Page
views:
54522
Programmatically creating GridView header row in
ASP.NET using C#
Hide demo Download
  In this article I’m going to explain how to create GridView header row programmatically in ASP.NET
using C#.
      I got one requirement that I have to create GridView header row programmatically. In my scenario I
have to create Multiple Header row which includes header style. It’s very easy once I figure out what the
proper event in the page lifecycle to use. I have used OnRowCreated="gvEmployee_RowCreated" event  to
achieve the task.
              In  this  demo  I  have  used  XML  to  bind  GridView.  After  binding  the  GridView  we  have  to  create
OnRowCreated  event.
 
Sample code: 
protected void gvEmployee_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
              …
             }
    }
 
 GridViewRow class is used to create GridView rows which includes DataRow, EmptyDatarow,
Footer, Header, Pager and Separator.
Dynamic Accordion
menu or Vertical menu
using jQuery in ASP.NET
MVC
Adding a Controller in
ASP.NET MVC 4
Creating ASP.NET MVC 4
web application with
Empty project template
Getting started with
ASP.NET MVC 4
Introduction to ASP.NET
MVC
Bind GridView using
jQuery in ASP.NET
Google Geo Chart or Geo
map with custom Color
and Tooltip in ASP.NET
Subscribe via Email
     
Recent Post
.NET News Weekly
Everything .NET, delivered straight to your inbox, every week.
Home ASP.NET Charts WCF XML AJAX JQuery Tips & Tricks
9/1/2015 Programmatically creating GridView header row in ASP.NET using C#
http://www.dotnetfox.com/articles/programmatically­creating­gridview­header­row­in­Asp­Net­using­C­Sharp­1059.aspx 2/6
 
Sample code:
GridViewRow HeaderRow = new GridViewRow(1, 0, DataControlRowType.Header,                                    
                                                                                                                                                   
                                          DataControlRowState.Insert);
             TableCell class is used to create cell which will be added to corresponding Header row.
Sample code:
             TableCell HeaderCell2 = new TableCell();
            HeaderCell2.Text = "Personal Details";
            HeaderCell2.ColumnSpan = 2;
            HeaderRow.Cells.Add(HeaderCell2);
 
          After we have to add Header row to the GridView. 
Sample code: 
gvEmployee.Controls[0].Controls.AddAt(0, HeaderRow);
 
Design your aspx page like this
Designer source code:
<%@ 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>gridview header</title> 
    <style type="text/css">
    .header
    {
         background­color:#3E3E3E;        
         font­family:Calibri;
         color:White;
         text­align:center;        
    }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
         <asp:GridView ID="gvEmployee" runat="server" AutoGenerateColumns="false" Width="600px"
                       OnRowCreated="gvEmployee_RowCreated" ShowHeader="false">        
         <RowStyle Font­Names="Calibri" />
         <Columns>
         <asp:BoundField DataField="empid" />
         <asp:BoundField DataField="name" />        
         <asp:BoundField DataField="city" />
GridView in ASP.NET
Be the first of your friends to like this
Dotnet fox
2,442 likes
Like Page Share
DotnetFox
+ 58
Follow +1
Labels
ASP.NET [97] Chart [27] GridView
[24] AJAX [15] JQuery [14] Google
chart [13] Fusion Charts [8] iTextSharp
[8] PDF [8] ASP.NET MVC [5] File
upload [5] Export [5] MVC [5] XML
[5] Import [4] DataList [4] MVC 4
[4] DropDownList [3] AJAX chart [3]
Accordian [3]
 
0
2
Like
15
 
5
 
0
 
Contact meAbout me2.4kLike
Follow @saran_31 1,555 followers
Be the first of your friends to like this
Dotnet fox
2,442 likes
Like Page Share
9/1/2015 Programmatically creating GridView header row in ASP.NET using C#
http://www.dotnetfox.com/articles/programmatically­creating­gridview­header­row­in­Asp­Net­using­C­Sharp­1059.aspx 3/6
         <asp:BoundField DataField="country" />    
         <asp:BoundField DataField="designation" />    
         <asp:BoundField DataField="joiningdate" />
         </Columns>
        </asp:GridView>
    </div>    
    </form>
</body>
</html>
 
C# code:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data; 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }
    protected void BindData()
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("EmployeeDetails.xml"));
        if (ds != null && ds.HasChanges())
        {
            gvEmployee.DataSource = ds;
            gvEmployee.DataBind();
        }
    }
 
    protected void gvEmployee_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
9/1/2015 Programmatically creating GridView header row in ASP.NET using C#
http://www.dotnetfox.com/articles/programmatically­creating­gridview­header­row­in­Asp­Net­using­C­Sharp­1059.aspx 4/6
        {
            GridViewRow HeaderRow = new GridViewRow(1, 0, DataControlRowType.Header,
DataControlRowState.Insert); 
            TableCell HeaderCell2 = new TableCell();
            HeaderCell2.Text = "Personal Details";
            HeaderCell2.ColumnSpan = 2;
            HeaderRow.Cells.Add(HeaderCell2);
 
            HeaderCell2 = new TableCell();
            HeaderCell2.Text = "Location";
            HeaderCell2.ColumnSpan = 2;
            HeaderRow.Cells.Add(HeaderCell2);
           
            HeaderCell2 = new TableCell();
            HeaderCell2.Text = "Office details";
            HeaderCell2.ColumnSpan = 2;
            HeaderRow.Cells.Add(HeaderCell2);
 
            gvEmployee.Controls[0].Controls.AddAt(0, HeaderRow);
 
            GridViewRow HeaderRow1 = new GridViewRow(0, 0, DataControlRowType.Header,
DataControlRowState.Insert);
 
            TableCell HeaderCell = new TableCell();
            HeaderCell.Text = "Employee­ID";           
            HeaderRow1.Cells.Add(HeaderCell);
 
            HeaderCell = new TableCell();
            HeaderCell.Text = "Name";           
            HeaderRow1.Cells.Add(HeaderCell);
                        
            HeaderCell = new TableCell();
            HeaderCell.Text = "City";         
            HeaderRow1.Cells.Add(HeaderCell);
 
            HeaderCell = new TableCell();
            HeaderCell.Text = "Country";           
            HeaderRow1.Cells.Add(HeaderCell);
 
            HeaderCell = new TableCell();
            HeaderCell.Text = "Designation";           
            HeaderRow1.Cells.Add(HeaderCell);
9/1/2015 Programmatically creating GridView header row in ASP.NET using C#
http://www.dotnetfox.com/articles/programmatically­creating­gridview­header­row­in­Asp­Net­using­C­Sharp­1059.aspx 5/6
Related Articles
 
            HeaderCell = new TableCell();
            HeaderCell.Text = "Joining date";
            HeaderRow1.Cells.Add(HeaderCell);
 
            HeaderRow.Attributes.Add("class", "header");
            HeaderRow1.Attributes.Add("class", "header");
            gvEmployee.Controls[0].Controls.AddAt(1, HeaderRow1);
        }
    }
}
View demo Download
If you enjoyed this article, get email updates (it's free).
Subscribe
Comments
How to add edit delete update records in Grid view ASP.NET
Introduction: In this article i'm going to explain how to add delete edit records in Grid view. Description: If we
want to add delete edit records in grid view we should use following grid view events, 1. onrowcommand 2.
onrowdeleting 3. onrowupdating 4. onrowcancelingedit 5. onrowediting Also we should use following template
field in Grid View. 1.ItemTemplate : ItemTemplate is used to display rec...
How to insert edit delete update records in Grid view using WCF
In this article i'm going to explain how to insert records using WCF. Windows Communication Foundation
(WCF) is an extension of the .NET framework to build and run connected systems. Windows Communication
Foundation provides a unified framework for building secure and reliable transacted Web services. Windows
Communication Foundation combines and extends the capabilities of Distributed System...
9/1/2015 Programmatically creating GridView header row in ASP.NET using C#
http://www.dotnetfox.com/articles/programmatically­creating­gridview­header­row­in­Asp­Net­using­C­Sharp­1059.aspx 6/6
Create dynamic GridView or
programmatically create ASP.NET
GridView with dynamic …2 comments • 2 years ago
Kiran — how do I dynamically add rows in
grid view windows application when press
button ? Please help me
GridView formatting or dynamically
change GridView cell color based on cell
value in …2 comments • 2 years ago
wadriano — Nice article, thanks. I used it in a
loop for all the cells of the gridview and it
works, here it is a sample code: if
(e.Row.RowType == …
Google Geo Chart or Geo map with custom
Color and Tooltip in ASP.NET ...
1 comment • a year ago
Mohammad Farahani — thanks
GridView custom CSS style example in
ASP.NET
4 comments • 2 years ago
hello — Thanks...
ALSO ON DOTNETFOX
2 Comments dotnetfox  Login1
 Share⤤ Sort by Best
Join the discussion…
• Reply •
karthik  •  a year ago
but Whie Exporting data to Excel Headers are not Visibel
 △ ▽  
• Reply •
Piyush Goriya  •  a year ago
Thanks a ,
you saved my life :D
I was in creitical situation, and your solution was the perfect one..
Thanks
 △ ▽  
WHAT'S THIS?
Subscribe✉ Add Disqus to your sited Privacyὑ
 Recommend
Share ›
Share ›
2.4kLike
Follow @saran_31 1,555 followers
Follow Dotnetfox Categories
ASP.NET
Charts
WCF
XML
AJAX
JQuery
Reference
msdn.microsoft.com
www.asp.net
blogs.technet.com
www.stackoverflow.com
www.jquery.com
Copyright © 2013 Dotnetfox.com Privacy Policy Terms and Conditions Disclaimer

More Related Content

Viewers also liked (6)

Walkthrough asp.net
Walkthrough asp.netWalkthrough asp.net
Walkthrough asp.net
 
00016335
0001633500016335
00016335
 
Schemas and soap_prt
Schemas and soap_prtSchemas and soap_prt
Schemas and soap_prt
 
sample tutorial
 sample tutorial  sample tutorial
sample tutorial
 
Cdn tutorial adcom
Cdn tutorial adcomCdn tutorial adcom
Cdn tutorial adcom
 
Cdn
CdnCdn
Cdn
 

Recently uploaded

CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceanilsa9823
 
Booking open Available Pune Call Girls Nanded City 6297143586 Call Hot India...
Booking open Available Pune Call Girls Nanded City  6297143586 Call Hot India...Booking open Available Pune Call Girls Nanded City  6297143586 Call Hot India...
Booking open Available Pune Call Girls Nanded City 6297143586 Call Hot India...Call Girls in Nagpur High Profile
 
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...ranjana rawat
 
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779Delhi Call girls
 
Cheap Rate Call girls Malviya Nagar 9205541914 shot 1500 night
Cheap Rate Call girls Malviya Nagar 9205541914 shot 1500 nightCheap Rate Call girls Malviya Nagar 9205541914 shot 1500 night
Cheap Rate Call girls Malviya Nagar 9205541914 shot 1500 nightDelhi Call girls
 
VIP Call Girls Service Kukatpally Hyderabad Call +91-8250192130
VIP Call Girls Service Kukatpally Hyderabad Call +91-8250192130VIP Call Girls Service Kukatpally Hyderabad Call +91-8250192130
VIP Call Girls Service Kukatpally Hyderabad Call +91-8250192130Suhani Kapoor
 
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service 🧵
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service  🧵CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service  🧵
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service 🧵anilsa9823
 
WAEC Carpentry and Joinery Past Questions
WAEC Carpentry and Joinery Past QuestionsWAEC Carpentry and Joinery Past Questions
WAEC Carpentry and Joinery Past QuestionsCharles Obaleagbon
 
SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxjanettecruzeiro1
 
VIP Kolkata Call Girl Gariahat 👉 8250192130 Available With Room
VIP Kolkata Call Girl Gariahat 👉 8250192130  Available With RoomVIP Kolkata Call Girl Gariahat 👉 8250192130  Available With Room
VIP Kolkata Call Girl Gariahat 👉 8250192130 Available With Roomdivyansh0kumar0
 
DragonBall PowerPoint Template for demo.pptx
DragonBall PowerPoint Template for demo.pptxDragonBall PowerPoint Template for demo.pptx
DragonBall PowerPoint Template for demo.pptxmirandajeremy200221
 
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service BhiwandiVIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service BhiwandiSuhani Kapoor
 
Fashion trends before and after covid.pptx
Fashion trends before and after covid.pptxFashion trends before and after covid.pptx
Fashion trends before and after covid.pptxVanshNarang19
 
Dubai Call Girls Pro Domain O525547819 Call Girls Dubai Doux
Dubai Call Girls Pro Domain O525547819 Call Girls Dubai DouxDubai Call Girls Pro Domain O525547819 Call Girls Dubai Doux
Dubai Call Girls Pro Domain O525547819 Call Girls Dubai Douxkojalkojal131
 
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdfThe_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdfAmirYakdi
 
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...Call Girls in Nagpur High Profile
 
Chapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdfChapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdfParomita Roy
 
The history of music videos a level presentation
The history of music videos a level presentationThe history of music videos a level presentation
The history of music videos a level presentationamedia6
 

Recently uploaded (20)

CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Aminabad Lucknow best Night Fun service
 
Booking open Available Pune Call Girls Nanded City 6297143586 Call Hot India...
Booking open Available Pune Call Girls Nanded City  6297143586 Call Hot India...Booking open Available Pune Call Girls Nanded City  6297143586 Call Hot India...
Booking open Available Pune Call Girls Nanded City 6297143586 Call Hot India...
 
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
(AISHA) Ambegaon Khurd Call Girls Just Call 7001035870 [ Cash on Delivery ] P...
 
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
Best VIP Call Girls Noida Sector 47 Call Me: 8448380779
 
Cheap Rate Call girls Malviya Nagar 9205541914 shot 1500 night
Cheap Rate Call girls Malviya Nagar 9205541914 shot 1500 nightCheap Rate Call girls Malviya Nagar 9205541914 shot 1500 night
Cheap Rate Call girls Malviya Nagar 9205541914 shot 1500 night
 
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
young call girls in Pandav nagar 🔝 9953056974 🔝 Delhi escort Service
 
VIP Call Girls Service Kukatpally Hyderabad Call +91-8250192130
VIP Call Girls Service Kukatpally Hyderabad Call +91-8250192130VIP Call Girls Service Kukatpally Hyderabad Call +91-8250192130
VIP Call Girls Service Kukatpally Hyderabad Call +91-8250192130
 
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service 🧵
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service  🧵CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service  🧵
CALL ON ➥8923113531 🔝Call Girls Kalyanpur Lucknow best Female service 🧵
 
WAEC Carpentry and Joinery Past Questions
WAEC Carpentry and Joinery Past QuestionsWAEC Carpentry and Joinery Past Questions
WAEC Carpentry and Joinery Past Questions
 
SD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptxSD_The MATATAG Curriculum Training Design.pptx
SD_The MATATAG Curriculum Training Design.pptx
 
VIP Kolkata Call Girl Gariahat 👉 8250192130 Available With Room
VIP Kolkata Call Girl Gariahat 👉 8250192130  Available With RoomVIP Kolkata Call Girl Gariahat 👉 8250192130  Available With Room
VIP Kolkata Call Girl Gariahat 👉 8250192130 Available With Room
 
DragonBall PowerPoint Template for demo.pptx
DragonBall PowerPoint Template for demo.pptxDragonBall PowerPoint Template for demo.pptx
DragonBall PowerPoint Template for demo.pptx
 
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service BhiwandiVIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
VIP Call Girls Bhiwandi Ananya 8250192130 Independent Escort Service Bhiwandi
 
Fashion trends before and after covid.pptx
Fashion trends before and after covid.pptxFashion trends before and after covid.pptx
Fashion trends before and after covid.pptx
 
Dubai Call Girls Pro Domain O525547819 Call Girls Dubai Doux
Dubai Call Girls Pro Domain O525547819 Call Girls Dubai DouxDubai Call Girls Pro Domain O525547819 Call Girls Dubai Doux
Dubai Call Girls Pro Domain O525547819 Call Girls Dubai Doux
 
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdfThe_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
The_Canvas_of_Creative_Mastery_Newsletter_April_2024_Version.pdf
 
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
VVIP Pune Call Girls Hadapsar (7001035870) Pune Escorts Nearby with Complete ...
 
Chapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdfChapter 19_DDA_TOD Policy_First Draft 2012.pdf
Chapter 19_DDA_TOD Policy_First Draft 2012.pdf
 
The history of music videos a level presentation
The history of music videos a level presentationThe history of music videos a level presentation
The history of music videos a level presentation
 
young call girls in Vivek Vihar🔝 9953056974 🔝 Delhi escort Service
young call girls in Vivek Vihar🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Vivek Vihar🔝 9953056974 🔝 Delhi escort Service
young call girls in Vivek Vihar🔝 9953056974 🔝 Delhi escort Service
 

Programmatically creating grid view header row in asp