Live Chat Icon For mobile
Live Chat Icon

How to change the HeaderText of the Datagrid

Platform: ASP.NET| Category: DataGrid

Method 1 : Set the HeaderText Property of the BoundColumn/TemplateColumn


<asp:DataGrid id='DataGrid1' AutoGenerateColumns='False' runat='server'>
<Columns>
	<asp:BoundColumn DataField='EmployeeID' HeaderText='Employee ID'>
	</asp:BoundColumn>
	<asp:BoundColumn DataField='FirstName' HeaderText='First Name'>
	</asp:BoundColumn>
	<asp:TemplateColumn HeaderText='LastName'>
	<ItemTemplate>
		<#%DataBinder.Eval(Container.DataItem, 'LastName').ToString()%>
	</ItemTemplate>
	</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

Method 2 : Dynamically change the HeaderText in the ItemDataBound Event of the DataGrid


<asp:DataGrid id='DataGrid1' OnItemDataBound='ItemDB' runat='server'></asp:DataGrid>

VB.NET


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
’Populate the DataGrid
End Sub

protected Sub ItemDB(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)  
If e.Item.ItemType = ListItemType.Header Then
	e.Item.Cells(0).Text = 'Employee ID'
	e.Item.Cells(1).Text = 'First Name'
	e.Item.Cells(2).Text = 'Last Name'
End If
End Sub

C#


private void Page_Load(object sender, System.EventArgs e)
{
//Populate the DataGrid
}

protected void ItemDB(Object sender  ,System.Web.UI.WebControls.DataGridItemEventArgs e   ) 
{
	if (e.Item.ItemType == ListItemType.Header)
	{
		e.Item.Cells[0].Text = 'Employee ID';
		e.Item.Cells[1].Text = 'First Name';
		e.Item.Cells[2].Text = 'Last Name';
	}
}

Share with

Share on twitter
Share on facebook
Share on linkedin