C# 更改 GridView 中列的标题文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13013241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:21:07  来源:igfitidea点击:

Change header text of columns in a GridView

c#asp.netgridview

提问by Mana

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,

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

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

and

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

does not seem to give me correct result.

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

采纳答案by Tim Schmelter

You should do that in GridView's RowDataBoundevent which is triggered for every GridViewRowafterit was databound.

您应该在 GridView 的RowDataBound事件中执行此操作,该事件在数据绑定GridViewRow每次都会触发。

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

or you can set AutogenerateColumnsto falseand add the columns declaratively on aspx:

或者您可以设置AutogenerateColumnsfalse并在 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>

回答by Ram Singh

You can do it with gridview's datarow bound event. try the following sample of code:

您可以使用 gridview 的数据行绑定事件来完成。尝试以下代码示例:

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

For more details about the row databound event study Thsi....

有关行数据绑定事件研究的更多详细信息Thsi....

回答by Amir Md Amiruzzaman

On your asp.net page add the gridview

在您的 asp.net 页面上添加 gridview

<asp:GridView ID="GridView1" onrowdatabound="GridView1_RowDataBound" >
</asp:GridView>

Create a method protected void method in your c# class called GridView1_RowDataBound

在名为 GridView1_RowDataBound 的 c# 类中创建一个方法 protected void 方法

as

作为

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

Everything should be working fine.

一切都应该正常工作。

回答by basim

I Think this Works:

我认为这有效:

 testGV.HeaderRow.Cells[0].Text="Date"

回答by Jitendra G2

Better to find cells from gridview instead of static/fix index so it will not generate any problem whenever you will add/remove any columns on gridview.

最好从 gridview 中查找单元格而不是静态/修复索引,这样无论何时您在 gridview 上添加/删除任何列都不会产生任何问题。

ASPX:

ASPX:

<asp:GridView ID="GridView1" OnRowDataBound="GridView1_RowDataBound" >
    <Columns>
        <asp:BoundField HeaderText="Date" DataField="CreatedDate" />
    </Columns>
</asp:GridView>

CS:

CS:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        for (int i = 0; i < e.Row.Cells.Count; i++)
        {
            if (string.Compare(e.Row.Cells[i].Text, "Date", true) == 0)
            {
                e.Row.Cells[i].Text = "Created Date";
            }
        }
    }
}

回答by JIYAUL MUSTAPHA

protected void grdDis_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            #region Dynamically Show gridView header From data base
            getAllheaderName();/*To get all Allowences master headerName*/

            TextBox txt_Days = (TextBox)grdDis.HeaderRow.FindControl("txtDays");
            txt_Days.Text = hidMonthsDays.Value;
            #endregion
        }
    }