Donate

WPF Datagrid Paging In VB.NET Using CollectionView Class

Based from the solution posted by timmyl here: How can I paginate a WPF DataGrid?. I managed to fix some bugs and added some functionalites such as MoveToFirstPage and MoveToLastPage.

Paging Class
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
Option Infer On  
 Imports System.Collections  
 Imports System.Collections.Generic  
 Imports System.ComponentModel  
 Imports System.Windows.Data  
 
 
 Public Class PageCollectionView  
   Inherits CollectionView  
   Private ReadOnly _innerList As IList  
   Private ReadOnly _itemsPerPage As Integer  
   Private _currentPage As Integer = 1  
   
 
   ' Constructor  
   Public Sub New(ByVal innerList As IList, ByVal itemsPerPage As Integer)  
     MyBase.New(innerList)  
     Me._innerList = innerList  
     Me._itemsPerPage = itemsPerPage  
   End Sub  
   
 
   Public Overrides ReadOnly Property Count() As Integer  
     Get  
       Return Me._itemsPerPage  
     End Get  
   End Property  
   
 
   Public Property CurrentPage() As Integer  
     Get  
       Return _currentPage  
     End Get  
     Set(ByVal value As Integer)  
       _currentPage = value  
       Me.OnPropertyChanged(New PropertyChangedEventArgs("CurrentPage"))  
     End Set  
   End Property
  
  
   Public ReadOnly Property ItemsPerPage() As Integer  
     Get  
       Return Me._itemsPerPage  
     End Get  
   End Property
   
  
   Public ReadOnly Property PageCount() As Integer  
     Get  
       Return (Me._innerList.Count + Me._itemsPerPage - 1) _  
           / Me._itemsPerPage  
     End Get  
   End Property  
   
 
   Private ReadOnly Property EndIndex() As Integer  
     Get  
       Dim _end = Me._currentPage * Me._itemsPerPage - 1  
       Return If(_end > Me._innerList.Count, Me._innerList.Count, _end)  
     End Get  
   End Property
 
  
   Private ReadOnly Property StartIndex() As Integer  
     Get  
       Return (Me._currentPage - 1) * Me._itemsPerPage  
     End Get  
   End Property
 
  
   Public Overrides Function GetItemAt(ByVal index As Integer) As Object  
     Dim offset = index Mod (Me._itemsPerPage)  
     If (((Me.StartIndex + offset) >= Me._innerList.Count)) Then  
       Return New Object  
     Else  
       Dim temp = Me._innerList(Me.StartIndex + offset)  
       Return Me._innerList(Me.StartIndex + offset)  
     End If  
   End Function  
 
 
   Public Sub MoveToFirstPage()  
     If Me._currentPage >= 1 Then  
       Me.CurrentPage = 1  
     End If  
     Me.Refresh()  
   End Sub  
 
 
   Public Sub MoveToPreviousPage()  
     If Me._currentPage > 1 Then  
       Me.CurrentPage -= 1  
     End If  
     Me.Refresh()  
   End Sub  
 
 
   Public Sub MoveToNextPage()  
     If Me._currentPage < Me.PageCount Then  
       Me.CurrentPage += 1  
     End If  
     Me.Refresh()  
   End Sub  
 
 
   Public Sub MoveToLastPage()  
     If Me._currentPage < Me.PageCount Then  
       Me.CurrentPage = Me.PageCount  
     End If  
     Me.Refresh()  
   End Sub  
 End Class

Screenshot:
WPF Datagrid Paging In VB.NET Using CollectionView Class
Note: I didnt include the source code for XAML declaration and the code behind since it can be converted easily.

Greg

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

TypeScript Error Or Bug: The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.

Invalid nested tag div found, expected closing tag input