i'm trying to add a new headerrow to a Gridview. This row should appear below the original headerrow.
As far as I know I have two events to choose from:
1.) Gridview_RowDataBound 2.) Gridview_RowCreated
Option 1 is not an option as the grid is not binding the data on each postback. Option 2 does not work as expected. I can add the row, but it is added before the HeaderRow because the HeaderRow itself is not added yet in this event...
Please assist, thank you!
Code: (InnerTable property is exposed by custom gridview)
Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.Header Then
Dim r As New GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal)
For Each c As DataControlField In CType(sender, GridView).Columns
Dim nc As New TableCell
nc.Text = c.AccessibleHeaderText
nc.BackColor = Drawing.Color.Cornsilk
r.Cells.Add(nc)
Next
Dim t As Table = GridView1.InnerTable
t.Controls.Add(r)
End If
End Sub
Since this is a custom GridView, why don't you consider overriding the CreateChildControls method?
I.e (sorry, C#):
UPDATE As was mentioned by Ropstah, the sniplet above does not work with pagination on. I moved the code to a PrepareControlHierarchy and now it works gracefully with pagination, selection, and sorting.
Try this when you add the row to the InnerTable:
Here's a quick basic test I did, which seems to work OK:
Nice work guys, I used your technique for grouping my AJAX enabled gridview, and I searched for a long, long time. Cheers.