How do I get the You can also use jQuery to add it. This avoids the problem with TableRowSection.TableHeader where gets dropped on PostBack. I use the following code to do this: The Otherwise (depending on how you render your grid) you'll throw exceptions like: The table must contain row sections in order of header, body and then footer. The I actually overrode the Asp.net GridView to make my own custom control, but you could paste this into your aspx.cs page and reference the GridView by name instead of using the custom-gridview approach. FYI: I haven't tested the footer logic, but I do know this works for Headers. This works for me: This was tried in VS2010. I know this is old, but, here's an interpretation of MikeTeeVee's answer, for a standard gridview: aspx page: aspx.cs: The code in the answer needs to go on I use this in GridView
control to render the
tags? I know
.UseAccessibleHeaders
makes it p
$('#myTableId').prepend($("<thead></thead>").append($(this).find("#myTableId tr:first")));
if
statements I added are important.
protected override void OnPreRender(EventArgs e)
{
if ( (this.ShowHeader == true && this.Rows.Count > 0)
|| (this.ShowHeaderWhenEmpty == true))
{
//Force GridView to use <thead> instead of <tbody> - 11/03/2013 - MCR.
this.HeaderRow.TableSection = TableRowSection.TableHeader;
}
if (this.ShowFooter == true && this.Rows.Count > 0)
{
//Force GridView to use <tfoot> instead of <tbody> - 11/03/2013 - MCR.
this.FooterRow.TableSection = TableRowSection.TableFooter;
}
base.OnPreRender(e);
}
this
object is my GridView.protected void GrdPagosRowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.TableSection = TableRowSection.TableBody;
}
else if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.TableSection = TableRowSection.TableHeader;
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.TableSection = TableRowSection.TableFooter;
}
}
<asp:GridView ID="GridView1" runat="server"
OnPreRender="GridView_PreRender">
protected void GridView_PreRender(object sender, EventArgs e)
{
GridView gv = (GridView)sender;
if ((gv.ShowHeader == true && gv.Rows.Count > 0)
|| (gv.ShowHeaderWhenEmpty == true))
{
//Force GridView to use <thead> instead of <tbody> - 11/03/2013 - MCR.
gv.HeaderRow.TableSection = TableRowSection.TableHeader;
}
if (gv.ShowFooter == true && gv.Rows.Count > 0)
{
//Force GridView to use <tfoot> instead of <tbody> - 11/03/2013 - MCR.
gv.FooterRow.TableSection = TableRowSection.TableFooter;
}
}
Page_Load
or GridView_PreRender
. I put it in a method that was called after Page_Load
and got a NullReferenceException
.OnRowDataBound
event:protected void GridViewResults_OnRowDataBound(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.Header) {
e.Row.TableSection = TableRowSection.TableHeader;
}
}