Advertisement
geoffreydv

Untitled

Apr 18th, 2011
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. Imports System.Collections
  2. Imports System.Data
  3. Imports System.Web.UI.WebControls
  4.  
  5. Namespace AlwaysShowHeaderFooter
  6.  
  7. Public Delegate Function MustAddARowHandler(ByVal data As IEnumerable) As IEnumerable
  8.  
  9.  
  10. Public Class GridViewAlwaysShow
  11. Inherits GridView
  12.  
  13. 'Flag used to identify if the datasource is empty.
  14. Private _isEmpty As Boolean = False
  15.  
  16. Protected Overrides Sub OnDataBound(ByVal e As EventArgs)
  17.  
  18. 'if in DesignMode, don't do anything special. Just call base and return.
  19. If DesignMode Then
  20. MyBase.OnDataBound(e)
  21. Return
  22. End If
  23.  
  24. 'hide the dummy row.
  25. If _isEmpty Then
  26. Rows(0).Visible = False
  27. End If
  28. MyBase.OnDataBound(e)
  29. End Sub
  30.  
  31. Protected Overrides Sub PerformDataBinding(ByVal data As IEnumerable)
  32.  
  33. 'If in DesignMode, don't do anything special. Just call base and return.
  34. If DesignMode Then
  35. MyBase.PerformDataBinding(data)
  36. Return
  37. End If
  38.  
  39. 'Count the data items.(I wish I knew a better way to do this.)
  40. Dim objectItemCount As Integer = 0
  41. For Each o As Object In data
  42. objectItemCount += 1
  43. Next
  44.  
  45. 'If there is a count, don't do anything special. Just call base and return.
  46. If objectItemCount > 0 Then
  47. MyBase.PerformDataBinding(data)
  48. Return
  49. End If
  50.  
  51. 'Set these values so the GridView knows what's up.
  52. SelectArguments.TotalRowCount += 1
  53. _isEmpty = True
  54.  
  55. 'If it's a DataView, it will work without having to handle the MustAddARow event.
  56. If data.[GetType]() Is GetType(DataView) Then
  57. 'Add a row and use that new view.
  58. Dim dv As DataView = DirectCast(data, DataView)
  59. dv.Table.Rows.InsertAt(dv.Table.NewRow(), 0)
  60. MyBase.PerformDataBinding(dv.Table.DefaultView)
  61. Return
  62. Else
  63. 'If you are using some custom object, you need to handle this event.
  64. MyBase.PerformDataBinding(OnMustAddARow(data))
  65. Return
  66. End If
  67. End Sub
  68.  
  69.  
  70. Protected Function OnMustAddARow(ByVal data As IEnumerable) As IEnumerable
  71. If MustAddARow Is Nothing Then
  72. Throw New NullReferenceException("The datasource has no rows. You must handle the ""MustAddARow"" Event.")
  73. End If
  74. Return MustAddARow(data)
  75. End Function
  76.  
  77. Public Event MustAddARow As MustAddARowHandler
  78.  
  79. End Class
  80.  
  81. End Namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement