【问题标题】:Change header text of columns in a GridView更改 GridView 中列的标题文本
【发布时间】:2012-10-12 08:52:17
【问题描述】:

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

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

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

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

【问题讨论】:

  • 您能否详细说明“没有帮助”?你有错误吗?您如何将数据绑定到网格?
  • 在从 SQL 中检索数据时,为什么不使用 AS 关键字更改列名?
  • 基本上我在使用,SqlDataAdapter DataSet --------- adapter.Fill(ds); testGV.DataSource = ds; testGV.DataBind(); ---------
  • 在你用来获取数据的查询或存储过程中改变它
  • 如果你允许对 gridview 进行排序,请使用 Sorted() 事件来处理下面的建议,否则将其放入 RowDataBound() 将不起作用

标签: c# asp.net gridview


【解决方案1】:

您应该在 GridView 的 RowDataBound 事件中执行此操作,该事件为每个 GridViewRow 数据绑定之后触发。

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

或者您可以将AutogenerateColumns 设置为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>

【讨论】:

  • 在从 SQL 检索时使用 As 关键字更改列名是更好的选择吗?不确定是否推荐
  • @TimSchmelter 谢谢你,我只是想确保它是一种很好的方法
  • 想知道,我如何调用函数 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  • 谢谢。帮助了我。
  • GridViewRowEventArgs 我得到这个名称类型的命名空间不知道的错误!!!我该如何解决它
【解决方案2】:

我认为这可行:

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

【讨论】:

  • 又短又甜。好答案!
  • 最佳答案!
  • 最适合放置在哪个 GridView 事件中?
  • 它在 DataBound 事件中对我有用,但我不知道这是否是它的最佳位置...它很慢...也许该事件被称为无数次...
  • PreRender 似乎更快。
【解决方案3】:

您可以使用 gridview 的数据行绑定事件来实现。试试下面的代码示例:

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

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

【讨论】:

    【解决方案4】:

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

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

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

    作为

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

    一切都应该正常。

    【讨论】:

      【解决方案5】:

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

      ASPX:

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

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

      【讨论】:

        【解决方案6】:
        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
                }
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-02-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多