Changing GridView Column HeaderText at runtime

Hm… Actually took me some time to figure out how to change the headertext at runtime for a column in a gridview.

But after som searching I found:

Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
If e.Row.RowType = DataControlRowType.Header Then
e.Row.Cells(1).Text = “Show me the money”
End If
End Sub

But, why can’t I just, when binding the gridview use: GridView1.Columns(1).HeaderText = “Show me the money”

20 Comments »

  1. pollo said,

    March 30, 2007 @ 3:29 pm

    Thanks for this – you saved me a lot of searching. You’re right – it’s not very intuitive.

  2. Dave said,

    May 27, 2007 @ 4:27 am

    Thank God for this answer.

    Why this isn’t a property of the gridview is beyond me.

  3. Naomi said,

    July 17, 2008 @ 10:34 pm

    Thank you for the post. Really strange why HeaderText is not working.

  4. Scott said,

    July 31, 2008 @ 10:30 pm

    yes thanks for the post. I also tried the headertext road and then found your post.

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.Header)
    {
    for (int i = 0; i < e.Row.Cells.Count;i++)
    {
    if (e.Row.Cells[i].Text == “CORP”)
    e.Row.Cells[i].Text = “WinCorp”;
    if (e.Row.Cells[i].Text == “PTNR”)
    e.Row.Cells[i].Text = “WinPtnr”;
    if (e.Row.Cells[i].Text == “SCORP”)
    e.Row.Cells[i].Text = “WinScorp”;
    if (e.Row.Cells[i].Text == “FIDUC”)
    e.Row.Cells[i].Text = “WinFiduc”;
    if (e.Row.Cells[i].Text == “WINTAX”)
    e.Row.Cells[i].Text = “WinTax”;
    if (e.Row.Cells[i].Text == “WINPAY”)
    e.Row.Cells[i].Text = “WinPay”;
    if (e.Row.Cells[i].Text == “DEPR”)
    e.Row.Cells[i].Text = “WinDepr”;
    if (e.Row.Cells[i].Text == “CORP”)
    e.Row.Cells[i].Text = “WinCorp”;
    }

    }

  5. zeeshan said,

    August 1, 2008 @ 10:06 pm

    I tried your code it solved the issue but it actually created another issue that column header text is no longer Sortable?

    Please help

  6. Jayce said,

    September 3, 2008 @ 8:02 am

    Thanks a lot. :)

  7. Rushabh said,

    September 19, 2008 @ 10:38 am

    Thank you.
    This was great help. Really saved my time, was very easy to implement.

  8. Rakesh said,

    January 29, 2009 @ 3:01 am

    thank u so much

  9. Alok said,

    February 19, 2009 @ 6:46 am

    thanks a lot.. is it a bug?

  10. Shridhar said,

    April 29, 2009 @ 8:19 am

    Exact Answer that i was looking for…….

  11. Redef said,

    May 19, 2009 @ 8:18 pm

    Great Post, usefulll 100% saved my life!

  12. Renee said,

    June 24, 2009 @ 9:35 am

    This post helped get me on the right track, but caused another issue – this approach will remove the link from sortable gridview columns, so here is how I eventually did it, thanks to help from:
    http://bytes.com/groups/net-asp/613783-changing-gridview-header-text

    (in rowdatabound sub)

    If e.Row.RowType = DataControlRowType.Header Then
    For Each myLB As LinkButton In e.Row.Cells(1).Controls
    myLB.Text = “User”
    Next
    End If

  13. Rashmi--RKC said,

    July 24, 2009 @ 7:39 am

    Hi,
    I think we can set thr HeaderText of a gridView from code-behind page using
    GridView1.Columns(1).HeaderText = “Show me the money”;

    Try to set this property before “GridView1.DataBind()”. This worked for me.

    Thanks,
    RKC

  14. Juergen Steinbaecker said,

    September 18, 2009 @ 3:18 pm

    You saved my day mate!!

    I had a Gridview nested inside a Formview, and no matter in what events I tried to change the Header (DataBound, PreRender, etc) nothing bloody worked, neither did GridView1.Columns(1).HeaderText .

    Tried it your way, and Voila!!

    So for whoever is using a Gridview inside a Formview and needs to change the headers at runtime, this is the way.

    Thanks again

  15. Patrick said,

    September 25, 2009 @ 9:09 pm

    Like Rashmi said in an earlier comment: Putting the

    GridView1.Columns(1).HeaderText = “Show me the money”;

    after the databind worked for me.

  16. TaponSayeem said,

    November 13, 2010 @ 9:32 am

    U can do it in any event of this page. And u don’t need any databind.

    GridView1.HeaderRow.Cells[10].Text=”New Text”;

  17. Farook said,

    April 30, 2011 @ 10:52 am

    Thanks Rashmi–RKC s

  18. dangphuc said,

    May 30, 2011 @ 8:41 am

    thanks TaponSayeem

  19. Anil said,

    September 8, 2011 @ 7:43 am

    well i was looking to same to. and finally i found a easy way that is.

    GridView1.HeaderRow.Cells(1).Text = “AAAAAAAA”

  20. ARTURO MARTINEZ said,

    January 10, 2012 @ 9:55 pm

    After five years your research is still helping many of us.

    Thank you for this usefull code.

RSS feed for comments on this post · TrackBack URI

Leave a Comment