I’m modernizing the UI of an old ASP.Net WebForms application. I make use of Bootstrap 3 to accomplish this. Most controls are easy to restyle but the GridView control makes some trouble. Bootstrap needs the table header to be in the thead section, but the GridView control renders the header inside the table body section. I was able to force GridView to render the header inside a thead element using this code:
public class AdvancedGridView : GridView
{
#region Overrides of GridView
protected override void OnPreRender( EventArgs e )
{
if ( ( ShowHeader && Rows.Count > 0 ) || ShowHeaderWhenEmpty )
HeaderRow.TableSection = TableRowSection.TableHeader;
if ( ShowFooter && Rows.Count > 0 )
FooterRow.TableSection = TableRowSection.TableFooter;
base.OnPreRender( e );
}
#endregion
}
But I still have a problem. As soon as the user sorts the table by any column, the GridView control will render again and move the header inside the table body element.
How can I make GridView render its header inside a thead element when it gets sorted by the user?
if (e.Row.RowType == DataControlRowType.Header) e.Row.TableSection = TableRowSection.TableHeader;- Daniel