将日期动态插入到 gridview header

Dynamically insert date into gridview header

我有一个 gridview 可以比较今年和去年的数据。

<Columns>
    <asp:BoundField DataField="st_entity_id" HeaderText="Entity ID" SortExpression="st_entity_id" />
    <asp:BoundField DataField="TotalConsentsThisYear" HeaderText="Total Consents for Year" ReadOnly="True" SortExpression="TotalConsentsThisYear" />
    <asp:BoundField DataField="TotalConsentsLastYear" HeaderText="Total Consents for last Year" ReadOnly="True" SortExpression="TotalConsentsLastYear" />
    <asp:BoundField DataField="TotalIssuedConsentsThisYear" HeaderText="Total Issued Consents for Year" ReadOnly="True" SortExpression="TotalIssuedConsentsThisYear" />
    <asp:BoundField DataField="TotalIssuedConsentsLastYear" HeaderText="Total Issued Consents for last Year" ReadOnly="True" SortExpression="TotalIssuedConsentsLastYear" />
    <asp:BoundField DataField="TotalInspectionsThisYear" HeaderText="Total Inspections for Year" ReadOnly="True" SortExpression="TotalInspectionsThisYear" />
    <asp:BoundField DataField="TotalInspectionsLastYear" HeaderText="Total Inspections for last Year" ReadOnly="True" SortExpression="TotalInspectionsLastYear" />
</Columns>

但是,我希望 gridview 显示 2014 年的总检查数,而不是 去年的总检查数。我知道我必须使用模板字段来执行此操作,但我不知道如何设置格式才能获得所需的结果。

这是我为网格视图的第一列所做的(根据您想要的任何列进行调整):

protected void Page_Load(object sender, EventArgs e)
{
    string curHeader = GridView1.HeaderRow.Cells[0].Text;
    GridView1.HeaderRow.Cells[0].Text = string.Format("{0} for {1}", curHeader, DateTime.Now.AddYears(-1).Year);
}

我得到的结果是: