How do I get Gridview to render THEAD?

C#.Netasp.netGridview

C# Problem Overview


How do I get the GridView control to render the <thead> <tbody> tags? I know .UseAccessibleHeaders makes it put <th> instead of <td>, but I cant get the <thead> to appear.

C# Solutions


Solution 1 - C#

This should do it:

gv.HeaderRow.TableSection = TableRowSection.TableHeader;

Solution 2 - C#

I use this in OnRowDataBound event:

protected void GridViewResults_OnRowDataBound(object sender, GridViewRowEventArgs e) {
    if (e.Row.RowType == DataControlRowType.Header) {
        e.Row.TableSection = TableRowSection.TableHeader;
    }
}

Solution 3 - C#

The code in the answer needs to go on Page_Load or GridView_PreRender. I put it in a method that was called after Page_Load and got a NullReferenceException.

Solution 4 - C#

I use the following code to do this:

The if statements I added are important.

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.

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);
}

The this object is my GridView.

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.

Solution 5 - C#

This works for me:

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;
    }
}

This was tried in VS2010.

Solution 6 - C#

I know this is old, but, here's an interpretation of MikeTeeVee's answer, for a standard gridview:

aspx page:

<asp:GridView ID="GridView1" runat="server" 
    OnPreRender="GridView_PreRender">

aspx.cs:

    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;
        }

    }

Solution 7 - C#

Create a function and use that function in your PageLoad event like this:

The function is:

private void MakeGridViewPrinterFriendly(GridView gridView) {  
    if (gridView.Rows.Count > 0) {          
        gridView.UseAccessibleHeader = true;  
        gridView.HeaderRow.TableSection = TableRowSection.TableHeader;  
    }  
} 

The PageLoad event is:

protected void Page_Load(object sender, EventArgs e) {
        if (!IsPostBack)
        {
            MakeGridViewPrinterFriendly(grddata);
        }
}

Solution 8 - C#

You can also use jQuery to add it. This avoids the problem with TableRowSection.TableHeader where gets dropped on PostBack.

$('#myTableId').prepend($("<thead></thead>").append($(this).find("#myTableId tr:first")));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndrew BullockView Question on Stackoverflow
Solution 1 - C#Phil JenkinsView Answer on Stackoverflow
Solution 2 - C#Neto KuhnView Answer on Stackoverflow
Solution 3 - C#ASalvoView Answer on Stackoverflow
Solution 4 - C#MikeTeeVeeView Answer on Stackoverflow
Solution 5 - C#Felipe DelgadoView Answer on Stackoverflow
Solution 6 - C#Jonathan HarrisView Answer on Stackoverflow
Solution 7 - C#RajpurohitView Answer on Stackoverflow
Solution 8 - C#MichaelView Answer on Stackoverflow