Obsidian Obsidian - 11 days ago 9
Vb.net Question

How to add a table row to thead section of a gridview?

CREATE A THEAD SECTION IN YOUR GRIDVIEW

Protected Overrides Sub OnPreRenderComplete(ByVal e As EventArgs)
If (gv.Rows.Count > 0) Then
gv.HeaderRow.TableSection = TableRowSection.TableHeader
End If
End Sub


INSERT A ROW WITH CONTENT INTO YOUR GRIDVIEW

Protected Sub gv_OnDataBound(sender As Object, e As EventArgs) Handles gv.DataBound
Dim row As New GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal)
For i As Integer = 0 To gv.Columns.Count - 1
Dim cell As New TableHeaderCell()
Dim txtBx As New TextBox()
cell.Controls.Add(txtBx)
row.Controls.Add(cell)
Next
gv.HeaderRow.Parent.Controls.AddAt(1, row)
End Sub
End Class


This will insert a row with a textbox within each cell
<th>
at row 1.




QUESTION



HOW TO INSERT A ROW (AS IN THE ABOVE EXAMPLE) INTO THE 'THEAD' SECTION OF A GRIDVIEW?

Doing this on the aspx page or in the vb is fine.

Answer

In your OnDataBound event, try setting the row you created to be in the "TableHeader" section (like you did in the PreRenderComplete event) prior to adding it to the GridView:

row.TableSection = TableRowSection.TableHeader

Since this sets the section of the table where the row belongs (header, footer, body), it should cause your row to wind up in the right place.