将 Gridview 列的标题居中对齐

To Align The Title of a Gridview Column to center

我有一个 GridView 。 我正在尝试将 headers 置于中心,但没有任何效果

 <asp:GridView ID="GridviewDataCoupon" runat="server" Style="text-align: center" Visible="false" ShowFooter="true" Width="99%"
                                AutoGenerateColumns="false">
                                <Columns>

                                <asp:TemplateField HeaderText="End Date" HeaderStyle-BackColor="#99CCCC">
                                        <ItemTemplate>
                                            <asp:Label ID="lbl4" runat="server" Text='<%# Eval("end_date") %>'>
                                            </asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>

                                <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
                                <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
                                <AlternatingRowStyle BackColor="WhiteSmoke" ForeColor="Black" />
                                <RowStyle BackColor="White" ForeColor="Black" />
                                <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
                                <SortedAscendingCellStyle BackColor="#EDF6F6" />
                                <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
                                <SortedDescendingCellStyle BackColor="#D6DFDF" />
                                <SortedDescendingHeaderStyle BackColor="#002876" />
                            </asp:GridView>

在其中一个 SO 问题中找到了这个解决方案

你可以这样做

尝试从 ASPX 代码向 GridView 添加 CssClass,并为 class.

指定以下样式
 <style type="text/css">
        .GridHeader td, .GridHeader th
        {
            text-align: center;
        }
    </style>

 <asp:GridView CssClass="GridHeader" runat="server">

这会将您的所有列文本设置为在您的 gridview 中居中