在GridView列更改标题文本 [英] Change header text of columns in a 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的<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx\"><$c$c>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";
}
}
也可以设置<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.autogeneratecolumns.aspx\"><$c$c>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>
这篇关于在GridView列更改标题文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!