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