将日期动态插入gridview标头

时间:2015-07-21 20:59:53

标签: asp.net templates gridview webforms

我有一个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年的总检查次数,而不是去年的总检查次数。我知道我必须使用模板字段来执行此操作,但我不知道如何格式化它以获得所需的结果。

1 个答案:

答案 0 :(得分:1)

这是我如何为网格视图的第一列做的(调整你想要的任何列):

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);
}

结果我得到了:

enter image description here