在GridView列更改标题文本

问题描述:

我有我用C#code编程绑定一个gridview。
问题是,列直接从数据库,可以看看网站上的奇数得到他们的标题文本,所以我想改变这种状况,但编程用C#code。
我已经试过了,

I have a gridview which i bind programmatically with c# code. The problem is, the columns get their header text directly from db which can look odd on websites, so i would like to change that but programmatically with c# code. i have already tried,

testGV.Columns[0].HeaderText = "Date";

this.testGV.Columns[0].HeaderText = "Date";

于事无补。

您应该做的,在GridView的RowDataBound$c$c>该事件被触发,每 GridViewRow 这是数据绑定。

You should do that in GridView's RowDataBound event which is triggered for every GridViewRow after it was databound.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        e.Row.Cells[0].Text = "Date";
    }
}

也可以设置AutogenerateColumns$c$c>到和声明上添加ASPX列:

or you can set AutogenerateColumns to false and add the columns declaratively on aspx:

<asp:gridview id="GridView1" 
  onrowdatabound="GridView1_RowDataBound"
  autogeneratecolumns="False"
  emptydatatext="No data available." 
   runat="server">
    <Columns>
         <asp:BoundField DataField="DateField" HeaderText="Date" 
            SortExpression="DateField" />
    </Columns>
</asp:gridview>