Love澤's Room

技術系ネタをまとめていたブログ。現在はカテゴリにこだわらず更新中。

GridViewでtheadを生成する方法

新サイトへ移転しました
約3秒後に自動的にリダイレクトします。

ASP.NETのGridViewコントロールは、デフォルトでは thead, tbody, tfootの要素を生成しない。
GridViewのRowCreatedイベントにおいて、Row.TableSelectionプロパティの設定を行うことで、thead, tbody, tfoot の要素を出力することが可能となる。

Protected Sub gvMeisai_RowCreated(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvMeisai.RowCreated
    Dim methodName As String = System.Reflection.MethodBase.GetCurrentMethod.Name
    Try
        '' 処理開始ログ
        WriteProcessLog(methodName, "開始")

        Select Case e.Row.RowType
            Case DataControlRowType.Header
                ' theadを出力
                e.Row.TableSection = TableRowSection.TableHeader
            Case DataControlRowType.DataRow
                ' tbodyを出力
                e.Row.TableSection = TableRowSection.TableBody
            Case DataControlRowType.Footer
                ' tfootを出力
                e.Row.TableSection = TableRowSection.TableFooter
        End Select
    Catch ex As Exception
        ' エラー処理
        WriteErrorLog(methodName, ex, ex.Message)
    Finally
        ' 処理終了ログ
        WriteProcessLog(methodName, "終了")
    End Try
End Sub