Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Gridview unable to update HeaderTemplate

Posted on 2008-11-05
5
Medium Priority
?
690 Views
Last Modified: 2012-08-13
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

0
Comment
Question by:rwallacej
  • 2
  • 2
5 Comments
 
LVL 37

Accepted Solution

by:
samtran0331 earned 1400 total points
ID: 22885438
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
 
LVL 18

Assisted Solution

by:David Robitaille
David Robitaille earned 600 total points
ID: 22885451
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
 
LVL 37

Expert Comment

by:samtran0331
ID: 22885462
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
 
LVL 18

Expert Comment

by:David Robitaille
ID: 22885488
well, samtran0331 you where faster than me!
rwallacej, you code will be more clear if you use FindControl like samtran0331 suggest.
0
 

Author Closing Comment

by:rwallacej
ID: 31513479
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses
Course of the Month11 days, 2 hours left to enroll

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question