Skip to content

Instantly share code, notes, and snippets.

@JayDouglass
Created May 15, 2013 14:57
Show Gist options
  • Save JayDouglass/5584610 to your computer and use it in GitHub Desktop.
Save JayDouglass/5584610 to your computer and use it in GitHub Desktop.
CustomPagedDataSource to allow setting TotalItems on a DataSource
Sub BindGrid(pageIndex As Integer)
gvSchoolDistricts.PageIndex = pageIndex
ValidateForm()
If FormState.IsValid Then
Dim pagedDataTable = schoolDistrictDataAccess.Query(pageIndex + 1, gvSchoolDistricts.PageSize, txtLEA.Text.NullIfEmpty(), txtFiscalYear.Text.Parse(Of Int16?), txtDistrictName.Text.NullIfEmpty())
'gvSchoolDistricts.VirtualItemCount = CInt(pagedDataTable.TotalItems)
'gvSchoolDistricts.DataSource = pagedDataTable.Table
gvSchoolDistricts.DataSource = pagedDataTable.ToPagedDataSource()
gvSchoolDistricts.DataBind()
End If
End Sub
Public Module PetaPocoExtensions
<Extension>
Public Function ToPagedDataSource(pagedDataTable As PagedDataTable) As CustomPagedDataSource
Return New CustomPagedDataSource(pagedDataTable.Table.AsDataView(), CInt(pagedDataTable.TotalItems))
End Function
End Module
''' <summary>
''' Allows setting the total items of a datasource
''' </summary>
''' <remarks></remarks>
Public Class CustomPagedDataSource
Implements IDataSource
Public Event DataSourceChanged(sender As Object, e As EventArgs) Implements IDataSource.DataSourceChanged
ReadOnly view As PagedDataSourceView
Public Sub New(items As IEnumerable, totalItems As Integer)
view = New PagedDataSourceView(Me, items, totalItems)
End Sub
Public Function GetView(viewName As String) As DataSourceView Implements IDataSource.GetView
Return view
End Function
Public Function GetViewNames() As ICollection Implements IDataSource.GetViewNames
Return New String() {""}
End Function
End Class
Public Class PagedDataSourceView
Inherits DataSourceView
Public Overrides ReadOnly Property CanRetrieveTotalRowCount() As Boolean
Get
Return True
End Get
End Property
Public Overrides ReadOnly Property CanSort() As Boolean
Get
Return True
End Get
End Property
Public Overrides ReadOnly Property CanPage() As Boolean
Get
Return True
End Get
End Property
Dim _totalItems As Integer
Public Property TotalItems() As Integer
Get
Return _totalItems
End Get
Private Set(value As Integer)
_totalItems = value
End Set
End Property
Dim _items As IEnumerable
Public Property Items() As IEnumerable
Get
Return _items
End Get
Private Set(value As IEnumerable)
_items = value
End Set
End Property
Public Sub New(owner As IDataSource, items As IEnumerable, totalItems As Integer)
MyBase.New(owner, "")
Me.Items = items
Me.TotalItems = totalItems
End Sub
Protected Overrides Function ExecuteSelect(arguments As DataSourceSelectArguments) As IEnumerable
' ignore paging arguments, assume Items already contains the items for the selected page
arguments.TotalRowCount = TotalItems ' TotalRowCount is a returned value
Return Items
End Function
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment