Click here to Skip to main content
13,404,383 members (44,408 online)
Click here to Skip to main content
Add your own
alternative version


24 bookmarked
Posted 14 Aug 2006

How to merge the header row for the GridView control

, 14 Aug 2006
Rate this:
Please Sign up or sign in to vote.
How to merge the header row for the GridView control.

Sample Image - Merge_GridView_Header.jpg


This very short article explains an easy way to merge the header columns for the GridView control. It works like the colspan property in HTML tables. This article is based on another one describing how to bind XML data to a GridView: How to bind an XML file to a GridView.

The Code

First, insert an event handler for the RowCreated event into your website. Then, check whether the new row is of type DataControlRowType.Header. If it is a header row, remove it and add a new TableCell.

Here is the code for this task:

Protected Sub GridView1_RowCreated(ByVal sender As Object, _
              ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) _
              Handles GridView1.RowCreated
    If e.Row.RowType = DataControlRowType.Header Then
        Dim oCell As New TableCell
        oCell.ColumnSpan = e.Row.Cells.Count
        oCell.Text = "Merged header"
    End If
End Sub

The ColumnSpan property is set to the amount of cells generated by the GridView. Next, remove the header using e.Row.Cells.Clear. Finally, assign the Text property for the new header and add it to the GridView.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
Germany Germany
Florian works as consultant for change- and configuration management for about 7 years. In this environment he is often forced to work with unix, perl and shell scripts.

For more information about change- and configuration management (espacially Serena Dimensions) visit:

For video tutorials about, ajax, gridviews, ... (in german) visit:

You may also be interested in...


Comments and Discussions

Generalgrid ctrl Pin
rahulbaisla21-Feb-08 20:07
memberrahulbaisla21-Feb-08 20:07 
QuestionMerging DataRows with RowSpan gives wierd output, why? Pin
Nhilesh B20-Dec-07 19:23
memberNhilesh B20-Dec-07 19:23 
Generalthanks! Pin
JeromeDeCuyper20-Feb-07 7:10
memberJeromeDeCuyper20-Feb-07 7:10 
Questionmerge cell Pin
o5ama16-Aug-06 0:03
membero5ama16-Aug-06 0:03 
how to merge header then in header had 2 cell, here is the sample :
| |merge header |
| |child header |
|1 | data 1 |
|2 | data 2 |
|3 | data 3 |

actualy i had been make it use
, and i want to convert it into <gridview>

here is my coding with table :

thank's before

AnswerRe: merge cell Pin
ayeleteric22-Nov-06 0:16
memberayeleteric22-Nov-06 0:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1802120.2 | Last Updated 15 Aug 2006
Article Copyright 2006 by fstrahberger
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid