Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET 2.0 - DataGrid with tbody / thead

Is there a way to get the DataGrid control to render the tbody and thead HTML elements?

like image 497
Ady Avatar asked Feb 18 '09 17:02

Ady


1 Answers

While I like the answer by "user186197", that blog post uses reflection, things might go wrong in non-full-trusted hosting environments. Here's what we use, no hacks:

public class THeadDataGrid : System.Web.UI.WebControls.DataGrid
{
    protected override void OnPreRender(EventArgs e)
    {
        this.UseAccessibleHeader = true; //to make sure we render TH, not TD

        Table table = Controls[0] as Table;

        if (table != null && table.Rows.Count > 0)
        {
            table.Rows[0].TableSection = TableRowSection.TableHeader;
            table.Rows[table.Rows.Count - 1].TableSection = TableRowSection.TableFooter;
        }

        base.OnPreRender(e);
    }
}
like image 139
Alex from Jitbit Avatar answered Oct 26 '22 23:10

Alex from Jitbit