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#
private void Sub Datagrid1_ItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e )
{
If (e.Item.ItemType = = ListItemType.Header)
{
e.Item.Cells.RemoveAt(2);
e.Item.Cells(1).ColumnSpan = 2;
e.Item.Cells(1).Text = "+
" =center>| Name |
" +
"| F Name | L" + " Name |
";
}
}
VB.NET
Private Sub Datagrid1_ItemCreated(ByVal sender As _
Object, ByVal e System.Web.UI.WebControls.DataGridItemEventArgs)_
Handles Datagrid1_ItemCreated
If e.Item.ItemType = ListItemType.Header Then
e.Item.Cells.RemoveAt(2)
e.Item.Cells(1).ColumnSpan = 2
e.Item.Cells(1).Text = "& _
" align =center>| Name |
" & _
"| F Name | L" & _ " Name |
"
End If
End Sub