Gridview unable to update HeaderTemplate

Hi,

I'm having problem updating the HeaderTemplate section of Gridview/BulkeditGridView.

After doing Gridview1.DataBind the RowDataBound is called and during this I want to manipulate the header template. I've placed a label in it to try.  All the data is filled in fine. I am binding to custom dataset not a database.

The code to change header never works though.

I added debug and found that
Me.GridView1.HeaderRow is always nothing

Because HeaderRow is nothing I can't find any controls in it or make changes.

AutoGenerateColumns is set to false.

Help please on this and how to change the header, thanks in advance.
<HeaderTemplate>
   <asp:Label ID="PeriodLbl" runat="server" Text="Period"></asp:Label>
</HeaderTemplate>
 
 Protected Sub Gridview1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound 
            'set column headers (units)
            If (e.Row.RowType = DataControlRowType.Header) Then
                Dim currentLbl As String
 
                'update the headers
                If Me.GridView1 IsNot Nothing AndAlso Me.GridView1.HeaderRow IsNot Nothing Then
                    For Each headerControl As DataControlFieldHeaderCell In Me.GridView1.HeaderRow.Controls
                        For Each ctrl2 As Control In headerControl.Controls
                            If TypeOf ctrl2 Is Label Then
                                currentLbl = CType(ctrl2, Label).Text
                                If currentLbl.EndsWith("UNIT") Then
                                    CType(ctrl2, Label).Text = currentLbl.Replace("UNIT", Me.lblFuel_Unit.Text)
                                    CType(ctrl2, Label).ForeColor = Color.Red
                                End If
                            End If
                        Next
                    Next
                End If
            End If
            End Sub

Open in new window

rwallacejAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

samtran0331Commented:
Try the below....
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.Header Then
            Dim currentLbl As Label
            currentLbl = CType(e.Row.FindControl("PeriodLbl"), Label)
            If currentLbl.Text.EndsWith("UNIT") Then
                currentLbl.Text = Me.lblFuel_Unit.Text
            End If
        End If
    End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David RobitailleAnalyst ProgrammerCommented:
since you have a if this way:
e.Row.RowType = DataControlRowType.Header
e.Row IS your header row.
so
For Each headerControl As DataControlFieldHeaderCell In e.Row.Controls
i m not sure if you need to use the HeaderRow like this, but if you could try it if you want...
For Each headerControl As DataControlFieldHeaderCell In e.Row.HeaderRow.Controls

 
0
samtran0331Commented:
Forgot the forecolor...so it would be:
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.Header Then
            Dim currentLbl As Label
            currentLbl = CType(e.Row.FindControl("PeriodLbl"), Label)
            If currentLbl.Text.EndsWith("UNIT") Then
                currentLbl.Text = Me.lblFuel_Unit.Text
                currentLbl.ForeColor = Drawing.Color.Red
            End If
        End If
    End Sub

Open in new window

0
David RobitailleAnalyst ProgrammerCommented:
well, samtran0331 you where faster than me!
rwallacej, you code will be more clear if you use FindControl like samtran0331 suggest.
0
rwallacejAuthor Commented:
thank-you very much for help, I'd got mixed up here but EE has saved me again
I have many fields ending in "UNIT" hence attempt to use for loop, but now I have just done FindControl for every field and it works

rwallacej
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.