Click here to Skip to main content
15,881,248 members
Articles / Web Development / ASP.NET
Article

Merging DataGrid Header Columns

Rate me:
Please Sign up or sign in to vote.
2.56/5 (10 votes)
24 Apr 2006 90.2K   22   5
This article describes the simple trick to merge the header columns of a DataGrid at run-time.

Introduction

This article describes a simple trick to merge the header columns of a DataGrid at run-time.

Background

I found a similar solution: Merge DataGrid Header, but I wanted to do this in 2-3 lines of code. So, I tried to use the members of the DataGrid to achieve the results.

Using the code

In the ItemCreated event of the DataGrid, catch the header item of the DataGrid. Suppose you want to merge n columns (cells of the header row), then remove n-1 cells from the header item. Then, make the column span property of the remaining cell equal to n. Then, add a table to the cell according to your requirements. The code below demonstrates the merging of two columns, which will result as shown in the above diagram.

C#

C#
private void  Sub Datagrid1_ItemCreated(object sender, 
        System.Web.UI.WebControls.DataGridItemEventArgs e )
{
    //If Header item
    
    If (e.Item.ItemType = = ListItemType.Header)
    {
        e.Item.Cells.RemoveAt(2);
        e.Item.Cells(1).ColumnSpan = 2;
        //Insert the table shown in the diagram 3
        // to the Text property of the Cell
        e.Item.Cells(1).Text = "<table style='FONT-WEIGHT: bold; WIDTH:" + 
              " 100%; COLOR: white; TEXT-ALIGN: center'><tr align" + 
              " =center><td colspan = 2 style='BORDER-BOTTOM:" + 
              " cccccc 1pt solid'>Name</td></tr>" + 
              "<tr align =center ><td style ='BORDER-RIGHT:" + 
              " cccccc 1pt solid'>F Name</td><td>L" + 
              " Name</td></tr></table>";
    }  
}

VB.NET

VB
Private Sub Datagrid1_ItemCreated(ByVal sender As _
         Object, ByVal e System.Web.UI.WebControls.DataGridItemEventArgs)_
         Handles Datagrid1_ItemCreated 
    'If Header item
    
    If e.Item.ItemType = ListItemType.Header Then
       e.Item.Cells.RemoveAt(2)
       e.Item.Cells(1).ColumnSpan = 2
       'Insert the table shown in the diagram 3 to the Text property of the Cell
       e.Item.Cells(1).Text = "<table style='FONT-WEIGHT: bold;" & _ 
         " WIDTH: 100%; COLOR: white; TEXT-ALIGN: center'><tr" & _ 
         " align =center><td colspan = 2 style='BORDER-BOTTOM:" & _ 
         " cccccc 1pt solid'>Name</td></tr>" & _
         "<tr align =center ><td style ='BORDER-RIGHT:" & _ 
         " cccccc 1pt solid'>F Name</td><td>L" & _ 
         " Name</td></tr></table>"
    End If  
End Sub

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
BugI am not finding ItemCreated Event of DataGridView Control in VS 2010 Pin
aarif moh shaikh16-Feb-16 1:37
professionalaarif moh shaikh16-Feb-16 1:37 
QuestionCell Merging In WPF DataGrid (Windows Application C#) Pin
tripathy.rajendra@gmail.com29-Sep-10 1:56
tripathy.rajendra@gmail.com29-Sep-10 1:56 
Hi.
Your article is helpful, but I need Cell Merging In WPF DataGrid (Windows Application C#).
Will you Guide me in this? for more detail u can see- http://img709.imageshack.us/img709/6213/cellmerged.jpg
Generalthis makes the table inaccessible Pin
isunshine29-Jul-08 4:58
isunshine29-Jul-08 4:58 
GeneralRe: this makes the table inaccessible Pin
Willem_6367-Oct-08 4:49
Willem_6367-Oct-08 4:49 
QuestionMerging vertically-rowspan Pin
Shivam Ray16-Aug-06 13:01
Shivam Ray16-Aug-06 13:01 

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.