' 元ネタ ' http://blogs.claritycon.com/blogs/kevin_marshall/archive/2006/02/28/247.aspx ' ' 変更点 ' ・C#からVBに移植 ' ・クラス名変更 ' ・Footerも表示 Imports System Imports System.Collections.Generic Imports System.ComponentModel Imports System.Text Imports System.Web.UI Imports System.Web.UI.WebControls Public Class GridViewX : Inherits GridView ' DataSourceにデータが無い場合に空テーブルを生成するかのブール値 _ Public Property ShowEmptyTable() As Boolean Get Dim o As Object = ViewState("ShowEmptyTable") If o IsNot Nothing Then Return o Else Return True End If End Get Set(ByVal value As Boolean) ViewState("ShowEmptyTable") = value End Set End Property ' 空テーブルのデータ行に表示するテキスト _ Public Property EmptyTableRowText() As String Get Dim o As Object = ViewState("EmptyTableRowText") If o IsNot Nothing Then Return o.ToString() Else Return "" End If End Get Set(ByVal value As String) ViewState("EmptyTableRowText") = value End Set End Property Protected Overrides Function CreateChildControls(ByVal dataSource As System.Collections.IEnumerable, ByVal dataBinding As Boolean) As Integer Dim numRows As Integer = MyBase.CreateChildControls(dataSource, dataBinding) ' データ行が無く、ShowEmptyTableがtrueなら、ヘッダフッタのみのテーブルを生成する If numRows = 0 And ShowEmptyTable Then ' テーブルを生成 Dim table As New Table() table.ID = Me.ID Dim row As GridViewRow Dim fields(Me.Columns.Count - 1) As DataControlField If Me.ShowHeader Then ' フッタ行を生成する row = Me.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal) ' 既存のカラムを配列に変換し、初期化する Me.Columns.CopyTo(fields, 0) Me.InitializeRow(row, fields) table.Rows.Add(row) End If ' 空の行を生成する row = New GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal) Dim cell As New TableCell() cell.ColumnSpan = Me.Columns.Count cell.Width = Unit.Percentage(100) cell.Controls.Add(New LiteralControl(EmptyTableRowText)) row.Cells.Add(cell) table.Rows.Add(row) If Me.ShowFooter Then ' フッタ行を生成する row = Me.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal) ' 既存のカラムを配列に変換し、初期化する Me.Columns.CopyTo(fields, 0) Me.InitializeRow(row, fields) table.Rows.Add(row) End If Me.Controls.Add(table) End If Return numRows End Function End Class