Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更改GridView中列的标题文本_C#_Asp.net_Gridview - Fatal编程技术网

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

C# 更改GridView中列的标题文本,c#,asp.net,gridview,C#,Asp.net,Gridview,我有一个GridView,我使用c代码以编程方式绑定它。 问题是,这些列直接从数据库中获取标题文本,在网站上显示时可能会显得很奇怪。因此,基本上,我想修改列标题文本,但需要编程。 我已经尝试了以下方法 testGV.Columns[0].HeaderText = "Date"; 及 似乎没有给我正确的结果。您应该在GridView的事件中这样做,该事件在数据绑定后为每个GridViewRow触发 protected void GridView1_RowDataBound(object send

我有一个GridView,我使用c代码以编程方式绑定它。 问题是,这些列直接从数据库中获取标题文本,在网站上显示时可能会显得很奇怪。因此,基本上,我想修改列标题文本,但需要编程。 我已经尝试了以下方法

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


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

您应该在GridView的事件中这样做,该事件在数据绑定后为每个
GridViewRow
触发

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        e.Row.Cells[0].Text = "Date";
    }
}
或者您可以设置为
false
,并在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的datarow绑定事件来执行此操作。请尝试以下代码示例:

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

有关asp.net页面上行数据绑定事件研究的详细信息,请添加gridview

<asp:GridView ID="GridView1" onrowdatabound="GridView1_RowDataBound" >
</asp:GridView>
一切都应该很好。

我认为这是可行的:

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

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

ASPX:


政务司司长:

受保护的无效GridView1\u行数据绑定(对象发送方,GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.Header)
{
for(int i=0;i
您能详细说明一下“没有帮助”吗?你有错误吗?如何将数据绑定到网格?为什么不在从SQL检索数据时使用AS关键字更改列名?基本上,我使用SqlDataAdapter数据集------adapter.Fill(ds);testGV.DataSource=ds;testGV.DataBind()---------在查询或用于获取数据的存储过程中更改它如果允许排序gridview,请使用Sorted()事件来处理以下建议,否则将其放入RowDataBound()将不起作用在从SQL检索时使用As关键字更改列名更好的选项?我不知道它是不是recommended@TimSchmelter谢谢,我只是想确定这是一种很好的方法。我想知道,如何调用受保护的函数void GridView1_RowDataBound(objectsender,GridViewRowEventArgs e)谢谢。Help me.GridViewRowEventArgs我得到一个错误,这个名称空间类型不知道!!!我该如何解决它的短小和甜蜜。回答得好!最好的答案!哪个GridView事件最适合?它在DataBound事件中对我有效,但我不知道这是否是它的最佳位置…它很慢…可能该事件被称为无数次…预渲染似乎更快。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        e.Row.Cells[0].Text = "HeaderText";
    }
}
 testGV.HeaderRow.Cells[0].Text="Date"
<asp:GridView ID="GridView1" OnRowDataBound="GridView1_RowDataBound" >
    <Columns>
        <asp:BoundField HeaderText="Date" DataField="CreatedDate" />
    </Columns>
</asp:GridView>
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";
            }
        }
    }
}
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
        }
    }