Imports System
Imports System.ComponentModel
Imports System.Text
Imports System.Web.UI
Imports System.Web.UI.WebControls

''' <summary>
''' This grid view extensds the <see cref="T:System.Web.UI.WebControls.GridView" /> and adds two properties.
''' </summary>
''' <remarks><see cref="GridView.ShowHeaderWhenEmpty" /> and <see cref="GridView.ShowFooterWhenEmpty" /> properties does what it says. When the grid doen't have any data to display this will help you decide whether do you want to show the header or footer.</remarks>
<ToolboxBitmap(GetType(GridView)), Description("A web GridView with two extra properties.")> _
Public Class GridView
    Inherits System.Web.UI.WebControls.GridView

#Region "Properties"

    ''' <summary>
    ''' Gets or sets a value indicating whether show header when there is no data to display.
    ''' </summary>
    ''' <value>
    ''' If <c>true</c>  then it will show header.
    ''' </value>
    <Category("Behavior")> _
    <Themeable(True)> _
    <Description("Enable or disable generating headers if no data rows in source")> _
    <Bindable(BindableSupport.No)> _
    Public Property ShowHeaderWhenEmpty() As Boolean
        Get
            If Me.ViewState("ShowHeaderWhenEmpty") Is Nothing Then
                Me.ViewState("ShowHeaderWhenEmpty") = False
            End If

            Return CBool(Me.ViewState("ShowHeaderWhenEmpty"))
        End Get
        Set(ByVal value As Boolean)
            Me.ViewState("ShowHeaderWhenEmpty") = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets a value indicating whether show footer when there is no data to display.
    ''' </summary>
    ''' <value>
    ''' If <c>true</c>  then it will show footer.
    ''' </value>
    <Category("Behavior")> _
    <Themeable(True)> _
    <Description("Enable or disable generating headers if no data rows in source")> _
    <Bindable(BindableSupport.No)> _
    Public Property ShowFooterWhenEmpty() As Boolean
        Get
            If Me.ViewState("ShowFooterWhenEmpty") Is Nothing Then
                Me.ViewState("ShowFooterWhenEmpty") = False
            End If

            Return CBool(Me.ViewState("ShowFooterWhenEmpty"))
        End Get
        Set(ByVal value As Boolean)
            Me.ViewState("ShowFooterWhenEmpty") = value
        End Set
    End Property
#End Region

    Protected Overloads Overrides Function CreateChildControls(ByVal dataSource As System.Collections.IEnumerable, ByVal dataBinding As Boolean) As Integer
        Dim rows As Integer = MyBase.CreateChildControls(dataSource, dataBinding)

        ' no data rows created, create empty table if enabled 
        If rows = 0 AndAlso (Me.ShowFooterWhenEmpty OrElse Me.ShowHeaderWhenEmpty) Then
            ' create the table 
            Dim table As Table = Me.CreateChildTable()

            Dim fields As DataControlField()
            If Me.AutoGenerateColumns Then
                Dim source As New PagedDataSource()
                source.DataSource = dataSource

                Dim autoGeneratedColumns As System.Collections.ICollection = Me.CreateColumns(source, True)
                fields = New DataControlField(autoGeneratedColumns.Count - 1) {}
                autoGeneratedColumns.CopyTo(fields, 0)
            Else
                fields = New DataControlField(Me.Columns.Count - 1) {}
                Me.Columns.CopyTo(fields, 0)
            End If

            If Me.ShowHeaderWhenEmpty Then
                ' create a new header row 
                Dim headerRow As GridViewRow = MyBase.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal)
                Me.InitializeRow(headerRow, fields)

                ' add the header row to the table 
                table.Rows.Add(headerRow)
            End If

            ' create the empty row 
            Dim emptyRow As New GridViewRow(-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal)
            Dim cell As New TableCell()
            cell.ColumnSpan = fields.Length
            cell.Width = Unit.Percentage(100)

            ' respect the precedence order if both EmptyDataTemplate 
            ' and EmptyDataText are both supplied ... 
            If Me.EmptyDataTemplate IsNot Nothing Then
                Me.EmptyDataTemplate.InstantiateIn(cell)
            ElseIf Not String.IsNullOrEmpty(Me.EmptyDataText) Then
                cell.Controls.Add(New LiteralControl(EmptyDataText))
            End If

            emptyRow.Cells.Add(cell)
            table.Rows.Add(emptyRow)

            If Me.ShowFooterWhenEmpty Then
                ' create footer row 
                Dim footerRow As GridViewRow = MyBase.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal)
                Me.InitializeRow(footerRow, fields)

                ' add the footer to the table 
                table.Rows.Add(footerRow)
            End If

            Me.Controls.Clear()
            Me.Controls.Add(table)
        End If

        Return rows
    End Function

End Class


