Here is an example code behind from one of my GridView's for doing just that in the GridView's PreRender event. In this example I'm actually adding two additional rows above the original Header. As you can see I'm adjusting the Colspans of the new cells. Forgive the VB:
Private Sub gvExpertRateHistory_PreRender(sender As Object, e As System.EventArgs) Handles gvExpertRateHistory.PreRender
Dim this As GridView = sender
Dim InnerTable As Table = If(this.HasControls(), this.Controls(0), Nothing)
If this.HeaderRow IsNot Nothing AndAlso InnerTable IsNot Nothing Then
Dim hr As GridViewRow
hr = New GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal)
hr.Cells.Add(NewCell(1, String.Empty, this, , HorizontalAlign.Left))
hr.Cells.Add(NewCell(2, "Requested On", this, , HorizontalAlign.Left))
hr.Cells.Add(NewCell(4, "Review Rates", this, "WhiteBorderLB"))
hr.Cells.Add(NewCell(6, "Court Rates", this, "WhiteBorderLB"))
hr.Cells.Add(NewCell(6, "Deposition Rates", this, "WhiteBorderLB"))
hr.Cells.Add(NewCell(4, "IME Rates", this, "WhiteBorderLB"))
InnerTable.Rows.AddAt(0, hr)
hr = New GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal)
hr.Cells.Add(NewCell(1, "Expert", this, , HorizontalAlign.Left))
hr.Cells.Add(NewCell(2, "Requested By", this, , HorizontalAlign.Left))
hr.Cells.Add(NewCell(2, "Hourly", this, "WhiteBorderLB"))
hr.Cells.Add(NewCell(2, "Flat", this, "WhiteBorderLB"))
hr.Cells.Add(NewCell(2, "Hourly", this, "WhiteBorderLB"))
hr.Cells.Add(NewCell(2, "Daily", this, "WhiteBorderLB"))
hr.Cells.Add(NewCell(2, "Half-Day", this, "WhiteBorderLB"))
hr.Cells.Add(NewCell(2, "Hourly", this, "WhiteBorderLB"))
hr.Cells.Add(NewCell(2, "Daily", this, "WhiteBorderLB"))
hr.Cells.Add(NewCell(2, "Half-Day", this, "WhiteBorderLB"))
hr.Cells.Add(NewCell(2, "Hourly", this, "WhiteBorderLB"))
hr.Cells.Add(NewCell(2, "Flat", this, "WhiteBorderLB"))
InnerTable.Rows.AddAt(1, hr)
End If
End Sub
This is a Helper function that makes it easier to add new cells.
Note:
- There is also a
RowSpan property in the TableHeaderCell class if needed
Also, AddCssClass() is a custom extension function of mine.
Private Function NewCell(colspan As Int32,
text As String,
gv As GridView,
Optional CssClass As String = "",
Optional Alignment As HorizontalAlign = HorizontalAlign.Center
) As TableHeaderCell
Dim thc As New TableHeaderCell
thc.HorizontalAlign = Alignment
thc.ColumnSpan = colspan
thc.Text = text
thc.BackColor = gv.HeaderRow.BackColor
thc.ForeColor = gv.HeaderRow.ForeColor
thc.Font.Bold = True
thc.AddCssClass(CssClass)
Return thc
End Function