Click here to Skip to main content
Click here to Skip to main content

How to merge the header row for the GridView control

By , 14 Aug 2006
 

Sample Image - Merge_GridView_Header.jpg

Introduction

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
        e.Row.Cells.Clear()
        oCell.Text = "Merged header"
        e.Row.Cells.Add(oCell)
    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.

License

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

About the Author

fstrahberger
Web Developer
Germany Germany
Member
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: www.venco.de
 
For video tutorials about asp.net, ajax, gridviews, ... (in german) visit: www.siore.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Generalgrid ctrlmemberrahulbaisla21 Feb '08 - 19:07 
QuestionMerging DataRows with RowSpan gives wierd output, why?memberNhilesh B20 Dec '07 - 18:23 
Generalthanks!memberJeromeDeCuyper20 Feb '07 - 6:10 
Questionmerge cellmembero5ama15 Aug '06 - 23:03 
AnswerRe: merge cellmemberayeleteric21 Nov '06 - 23:16 
Please open following link
http://yoni.minicircuits.com/DetailMixer.aspx?Flag=0&PS=sm&FreqRFLO_Low=540&FreqRFLO_High=580&CLossMax=7[^]
 
You can see a complicated header in the gridview.
What I need is to allow sorting for each column (for the last header row).
Notice that I add code gridview1.showheader=false, I mean that the original header of the gridview is hided.
I add the complicated header in code. How can I add possibility to each tablecell to be a sort function?
 
PLease help
 

 
Eric H.

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

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