vb.net小白的開發日常
研究了一整天的vb.net分頁功能,發現這個需求不用做。。。(是該開心呢?還是開心呢?)
研究了一整天也算是研究出來了
由於不熟悉vb.net和DataGridView控件的使用,所以進行了較長時間的百度型研究;
經過一段時間的分析,我決定通過建立一個PageDataGridViewPage(命名就這水平了…)類,通過實例化該類實現分頁功能(一堆廢話湊字數…)
上代碼:
Imports System.Collections.Generic
Imports DentalLibrary.ShowToothPosition
Public Class PageDataGridViewPage
'<---test---->
Private _RowsPerPage As Integer '每頁記錄數
Private _TotalPage As Integer '總頁數
Private _CurrentPage As Integer '當前頁數
Private _tempDetail As New List(Of DeptProcessDetail) '要分頁的List
'Private _dv As DataView '需要分頁顯示的DataView
'<----test--->
'<---test---->
'獲取與設置每頁記錄數
Public Property RowsPerPage() As Integer
Get
Return _RowsPerPage
End Get
Set(ByVal value As Integer)
_RowsPerPage = value
End Set
End Property
'獲取總頁數
Public Property TotalPage() As Integer
Get
Return _TotalPage
End Get
Set(value As Integer)
_TotalPage = value
End Set
End Property
'獲取與設置當前頁數
Public Property CurrentPage() As Integer
Get
Return _CurrentPage
End Get
Set(ByVal value As Integer)
_CurrentPage = value
End Set
End Property
'設置需要分頁的SetDataGridView
Public Property SetTempDetail() As List(Of DeptProcessDetail)
Get
Return _tempDetail
End Get
Set(ByVal value As List(Of DeptProcessDetail))
_tempDetail = value
End Set
End Property
'設置分頁顯示的DataView
'Public WriteOnly Property SetDataView() As DataView
' Set(ByVal value As DataView)
' _dv = value
' End Set
'End Property
Public Sub New()
' 此調用是設計器所必需的。
'InitializeComponent()
' 在 InitializeComponent() 調用之後添加任何初始化。
End Sub
'重載New函數,在構造時對成員賦值
Public Sub New(ByVal TempDetailLst As List(Of DeptProcessDetail), ByVal RowsPerPage As Integer)
_tempDetail = TempDetailLst
_RowsPerPage = RowsPerPage
End Sub
'分頁
Public Sub Paging()
'判斷DataView中的記錄數是否足夠形成多頁,
'若不能,則只有一頁
If _tempDetail.Count <= _RowsPerPage Then
_TotalPage = 1
GoLastPage()
Exit Sub
End If
'若能分多頁
If _tempDetail.Count Mod _RowsPerPage = 0 Then
_TotalPage = Int(_tempDetail.Count / _RowsPerPage)
Else
_TotalPage = Int(_tempDetail.Count / _RowsPerPage) + 1
End If
GoFirstPage()
End Sub
'到第一頁
Public Sub GoFirstPage()
'若只有一頁
If _TotalPage = 1 Then
GoLastPage()
Exit Sub
End If
'若有多頁
_CurrentPage = 0
GoNoPage(_CurrentPage)
End Sub
Public Sub GoNextPage()
_CurrentPage += 1
If _CurrentPage > _TotalPage - 1 Then
_CurrentPage = _TotalPage - 1
Exit Sub
End If
'若已達最後一頁,則顯示最後一頁
If _CurrentPage = _TotalPage - 1 Then
GoLastPage()
Exit Sub
End If
'若未到最後一頁,就到指定下一頁
GoNoPage(_CurrentPage)
End Sub
Public Sub GoPrevPage()
_CurrentPage -= 1
If _CurrentPage < 0 Then
_CurrentPage = 0
Exit Sub
End If
GoNoPage(_CurrentPage)
End Sub
Public Sub GoLastPage()
Dim LastTempDetail As New List(Of DeptProcessDetail)
LastTempDetail = _tempDetail.GetRange((TotalPage - 1) * _RowsPerPage, _tempDetail.Count)
_tempDetail = LastTempDetail
End Sub
Public Sub GoNoPage(ByVal PageNo As Integer)
_CurrentPage = PageNo
If _CurrentPage < 0 Then
MsgBox("頁號不能小於1")
Exit Sub
End If
'防止頁號溢出
If _CurrentPage >= _TotalPage Then
MsgBox("頁號超出上限")
Exit Sub
End If
'頁號爲最後一頁
If _CurrentPage = _TotalPage - 1 Then
GoLastPage()
Exit Sub
End If
'不是最後一頁
Dim NotLastTempDetail As New List(Of DeptProcessDetail)
Dim startLocation As Integer = PageNo * _RowsPerPage
Dim endLocation As Integer = (PageNo + 1) * _RowsPerPage
If endLocation < RowsPerPage Then
NotLastTempDetail = _tempDetail.GetRange(startLocation, endLocation)
Else
NotLastTempDetail = _tempDetail.GetRange(startLocation, RowsPerPage)
End If
_tempDetail = NotLastTempDetail
End Sub
End Class