Live Chat Icon For mobile
Live Chat Icon

How to create a Main Header in DataGrid along with the DataGrid Column Headers

Platform: ASP.NET| Category: DataGrid

You have to manually add a row to the table generated by the datagrid as follows.


<asp:DataGrid id='DataGrid1' OnPreRender ='dgPreRender' runat='server'></asp:DataGrid>

VB.NET


protected Sub dgPreRender(ByVal sender As Object, ByVal e As System.EventArgs)  
      	Dim dgItem As New DataGridItem(0, 0, ListItemType.Header)
      	Dim tbCell As New TableCell
	tbCell.ColumnSpan = 3 ’Set it to the colspan that you want
	tbCell.Text = 'Category Information'
	tbCell.Attributes.Add('style', 'text-align:center')
	dgItem.Cells.Add(tbCell)
	DataGrid1.Controls(0).Controls.AddAt(0, dgItem)
End Sub

C#


protected void dgPreRender(object sender, System.EventArgs e  ) 
{
	DataGridItem dgItem = new DataGridItem (0, 0, ListItemType.Header);
	TableCell tbCell = new TableCell();
	tbCell.ColumnSpan = 3;// Set it to the colspan that you want
	tbCell.Text = 'Category Information';
	tbCell.Attributes.Add('style', 'text-align:center');
	dgItem.Cells.Add(tbCell);
	DataGrid1.Controls[0].Controls.AddAt(0, dgItem);
}

Share with

Share on twitter
Share on facebook
Share on linkedin