About Us | Contact Us 
SEARCH in GO  
 

Welcome to MIND Sign in | Join | Help

ASP.Net display GridView header and footer when the data source is empty.

Last post 05-17-2008, 12:13 PM by Firedancer. 2 replies.
Sort Posts: Previous Next
  •  04-24-2008, 3:57 PM 3103

    ASP.Net display GridView header and footer when the data source is empty.

    Today, I experience that GridView won't show when data source is empty.

    I google it and found that Joe Wrobel article is very useful.

    I have convert his c# code to vb.net code....

    Imports System
    Imports System.Collections
    Imports System.Data
    Imports System.Web.UI.WebControls

    Namespace AlwaysShowHeaderFooter

        Public Delegate Function MustAddARowHandler(ByVal data As IEnumerable) As IEnumerable

        Public class GridViewAlwaysShow
            Inherits GridView

            ' Flag used to identify if the datasource is empty.
            Dim _isEmpty As Boolean = False

            Protected Overrides Sub OnDataBound(ByVal e As EventArgs)
                ' if in DesignMode, don't do anything special. Just call base and return.
                If (DesignMode) Then
                    MyBase.OnDataBound(e)
                    return
                End If

                ' hide the dummy row.
                If (_isEmpty) Then
                    Rows(0).Visible = False
                End If
                MyBase.OnDataBound(e)
            End Sub

            Protected Overrides Sub PerformDataBinding(ByVal data As IEnumerable)

                ' If in DesignMode, don't do anything special. Just call base and return.
                If (DesignMode) Then
                    MyBase.PerformDataBinding(data)
                    return
                End If
               
                ' Count the data items.(I wish I knew a better way to do this.)
                Dim objectItemCount As Integer = 0
                Dim o As Object
                For Each o in data
                    objectItemCount+=1
                Next

                ' If there is a count, don't do anything special. Just call base and return.
                If (objectItemCount > 0) Then
                    MyBase.PerformDataBinding(data)
                    return
                End If
               
                ' Set these values so the GridView knows what's up.
                SelectArguments.TotalRowCount+=1
                _isEmpty = True

                ' If it's a DataView, it will work without having to handle the MustAddARow event.
                If (TypeOf(data) Is DataView) Then
                    ' Add a row and use that new view.
                    Dim dv As DataView = CType(data, DataView)
                    dv.Table.Rows.InsertAt(dv.Table.NewRow(), 0)
                    MyBase.PerformDataBinding(dv.Table.DefaultView)
                    return
                Else
                    ' If you are using some custom object, you need to handle this event.
                    MyBase.PerformDataBinding(OnMustAddARow(data))
                    return
                End If
            End Sub


            Protected Function OnMustAddARow(ByVal data As IEnumerable) As IEnumerable
                if (MustAddARow Is Nothing) Then
                    Throw New NullReferenceException("The datasource has no rows. You must handle the ""MustAddARow"" Event.")
                End If
                return MustAddARow(data)
            End Function

            Public MustAddARow As MustAddARowHandler

        End Class

    End Namespace

  •  04-24-2008, 6:28 PM 3107 in reply to 3103

    Re: ASP.Net display GridView header and footer when the data source is empty.

    Keep it up and thanks for contributing. Probably you can post at the article's author blog ... handy for others to use it.
    Best Regards,

    Chua Wen Ching
    Software Engineer Mesiniaga Berhad
    President of MIND Community
    Head of Academics for INETA Malaysia (APAC region)
    Microsoft Most Valuable Professional (MVP) - Visual Developer - Visual C#
  •  05-17-2008, 12:13 PM 3119 in reply to 3103

    Re: ASP.Net display GridView header and footer when the data source is empty.

    You can use the EmptyDataRowStyle and EmptyDataTemplate to format the display of your GridView when there is no rows in your data source.
    Blog: http://serena-yeoh.blogspot.com
View as RSS news feed in XML
 ©2006 Mind Community. All rights reserved. | Privacy Statements