Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.
- Private _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
- For Each o As Object 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 data.[GetType]() Is GetType(DataView) Then
- 'Add a row and use that new view.
- Dim dv As DataView = DirectCast(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 Event MustAddARow As MustAddARowHandler
- End Class
- End Namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement