Skip to content

Instantly share code, notes, and snippets.

@abraxas86
Created March 12, 2024 19:29
Show Gist options
  • Save abraxas86/a255761e9796fdf99f625f1f26c0538a to your computer and use it in GitHub Desktop.
Save abraxas86/a255761e9796fdf99f625f1f26c0538a to your computer and use it in GitHub Desktop.
VB.NET codebehind to fix GridView Tables so the header row is in a proper <thead> section instead of <tbody>
Public Class GvHelpers
''' <summary>
''' ASP automatically throws &lt;th&gt; cells in a &lt;tbody&gt; grouping making it tricky to target specific areas of tables
''' with CSS and Javascript. This method is used to break the first row of the provded gridview into a proper &lt;thead&gt; section.
''' It is recommended that you call this method on RowCreated to ensure the header is kept separate as the user modifies/browses the table,
''' otherwise things like flipping between pages would cause the header to get sandwiched back in a &lt;tbody&gt; grouping.
''' </summary>
''' <param name="gridView">The gridview to separate the header from</param>
''' <remarks>
''' Example:
''' Let's target a gridview defined as "gvMyGridview":
''' <code>
''' Protected Sub gvMyGridview_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles gvMyGridview.RowCreated
''' separateGvHeader(gvMyGridview)
''' End Sub
''' </code>
''' </remarks>
Public Shared Sub separateGvHeader(ByVal gridView As GridView)
If gridView.Rows.Count > 0 AndAlso gridView.HeaderRow IsNot Nothing Then
gridView.UseAccessibleHeader = True
gridView.HeaderRow.TableSection = TableRowSection.TableHeader
End If
End Sub
End Class
@abraxas86
Copy link
Author

I know I pluralized the class... I was thinking ahead to if I had any other tools I'd need to add.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment