DreamTeK
DreamTeK

Reputation: 34177

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.

Upvotes: 2

Views: 2877

Answers (2)

Josh Darnell
Josh Darnell

Reputation: 11433

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.

Upvotes: 1

DreamTeK
DreamTeK

Reputation: 34177

I couldn't find a way to do this in vb. To apply content to the existing tr th row in the thead section you can use.

  <asp:TemplateField>
    <HeaderTemplate>
      <p>COL TITLE</p>
      <asp:TextBox ID="txBx" runat="server"/>
    </HeaderTemplate>
    <ItemTemplate>
      CELL DATA
    </ItemTemplate>
  </asp:TemplateField>

Which will result in:

    <thead>
      <tr>
        <th>
          <p>COL TITLE</p>
          <input id="txBx" type="text"/>
        </th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          COL DATA
        </td>
      </tr>
    </tbody>

Upvotes: 1

Related Questions