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

查看:30
本文介绍了更改 GridView 中列的标题文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 c# 代码以编程方式绑定的 GridView.问题是,这些列直接从数据库中获取其标题文本,这在网站上呈现时可能看起来很奇怪.所以基本上,我想修改列标题文本,但以编程方式.我已经尝试过以下方法,

I have a GridView which i programmatically bind using c# code. The problem is, the columns get their header texts directly from Database, which can look odd when presented on websites. So basically, i would like to modify the column header text, but programmatically. i have already tried the following,

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

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

似乎没有给我正确的结果.

does not seem to give me correct result.

推荐答案

您应该在 GridView 的 RowDataBound 事件,它为每个 GridViewRow after 触发数据绑定.

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

或者你可以设置AutogenerateColumnsfalse 并在 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天全站免登陆