How to centrally align the header text of a TemplateField?

The <center> tag is deprecated in HTML 4.01 and not supported in HTML5 - the working code you posted could be "CSS-ified" as follows:

<asp:TemplateField ItemStyle-HorizontalAlign="Center">
    <HeaderTemplate>
        <asp:Panel style="margin-left: auto; margin-right: auto; text-align: center;">
            Events
        <asp:Panel>
    </HeaderTemplate>
<asp:TemplateField>

(Note: Panel is the ASP.Net equivalent of a <div>.)

A slight improvement here is to define a CSS class for the style so it can be reused elsewhere:

.center {
  margin-left: auto;
  margin-right: auto;
  text-align: center;
}

...and reference it from the panel instead of using the inline style:

<asp:Panel CssClass="center">

I have just created a new WebForms solution and removed bootstrap just to be sure that no css styles interfere with my code. This is what I've done to reproduce your problem.

aspx:

<asp:GridView runat="server" ID="grid" Style="width: 500px;">
    <Columns>
        <asp:TemplateField HeaderText="FirstName - TemplateField">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>

</asp:GridView>

As you can see, I have defined one TemplateField without any additional css styles.

CodeBehind:

public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        grid.DataSource = GetPersons();
        grid.DataBind();
    }

    public IEnumerable<Person> GetPersons()
    {
        for(int i = 0; i< 10; i++)
        {
            yield return new Person { FirstName = $"John{i}", LastName = "Doe", Age = i };
        }
    }
}

I am just returning 10 dummy items to create a demo grid. Nothing out of the ordinary.

This is the result in Chrome and Internet Explorer:

enter image description here

As you can see, the headers are centered by default. And this is for both -BoundFields and TemaplateFields.

If this is not the case for you, I recommend checking if any other stylesheets are interfering with your styles. I know that bootstrap 3 defaults to text-align: center for th elements (Because I just checked)


By right, temStyle-HorizontalAlign="Center" should be working. Just be aware that your gridview styles are inherited from parent stylesheet. Meaning, your gridview has at least one parent style which is not certer align. That should be where your problem comes.