在GridView列更改标题文本 [英] Change header text of columns in a GridView

查看:222
本文介绍了在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该事件被触发,每 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和声明上添加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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆