ASP.net change header text  of GridView  template columns

Murray Brown
Murray Brown used Ask the Experts™
on
Hi.  I am trying to change the header text of template columns in a GridView in ASP.net using the following VB.net code but for some reason the
headers are not changing

    Private Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
        For i As Integer = 0 To UBound(arrDates)
            oColIndex = i + 2
            oTemplateColumn = "D" & CStr(i + 1)
            oEachDate = CDate(arrDates(i))
            oDate = oEachDate.ToString("dd MMM yyyy")
            Me.GridView1.Columns(oColIndex).Visible = True
            If e.Row.RowType = DataControlRowType.Header Then
                Me.GridView1.Columns(oColIndex).HeaderText = oDate
            End If
        End if
Ebd Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Obadiah ChristopherDeveloper User Interface

Commented:
but for some reason the
headers are changing
-- Not sure what this means.

You do not need to write this code inside gridview rowdatabound. You can write it after your code to gridview1.DataBind();
For i As Integer = 0 To UBound(arrDates)
            oColIndex = i + 2
            oTemplateColumn = "D" & CStr(i + 1)
            oEachDate = CDate(arrDates(i))
            oDate = oEachDate.ToString("dd MMM yyyy")
            Me.GridView1.Columns(oColIndex).Visible = True
            Me.GridView1.Columns(oColIndex).HeaderText = oDate
        End if

Open in new window

Murray BrownASP.net/VSTO Developer

Author

Commented:
sorry missed the word "not"
ASP.net/VSTO Developer
Commented:
This worked for me in the RowDataBound event
  If e.Row.RowType = DataControlRowType.Header Then
                    e.Row.Cells(oColIndex).Text = oDateForHeader
   End If

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial