16,547
社区成员
发帖
与我相关
我的任务
分享
Public Class ClsDataGridViewPage
Private _RowsPerPage As Integer '每页记录数
Private _TotalPage As Integer '总页数
Private _curPage As Integer = 0 '当前页数
Private _DataGridView As Windows.Forms.DataGridView '要分页的DataGridView
Private _dv As DataView '与需要分页显示的的DataView
Public Property RowSsPerPage() As Integer '获取与设置每页记录数
Get
Return _RowsPerPage
End Get
Set(ByVal value As Integer)
_RowsPerPage = value
End Set
End Property
'获取总页数
Public ReadOnly Property TotalPage() As Integer
Get
Return _TotalPage
End Get
End Property
'获取与设置当前页数
Public Property curPage() As Integer
Get
Return _curPage
End Get
Set(ByVal value As Integer)
_curPage = value
End Set
End Property
'设置需要分页的GetDataGridView
Public WriteOnly Property SetDataGridView()
Set(ByVal value As Object)
_DataGridView = value
End Set
End Property
'设置需要分页显示的的DataView
Public WriteOnly Property SetDataView()
Set(ByVal value As Object)
_dv = value
End Set
End Property
Public Sub New()
End Sub
'重载NEW函数,在构造时就可以对成员赋值
Public Sub New(ByVal datagridview As Windows.Forms.DataGridView, ByVal dv As DataView, ByVal RowsPerPage As Integer)
_DataGridView = datagridview
_dv = dv
_RowsPerPage = RowsPerPage
End Sub
'开始分页啦
Public Sub Paging()
'首先判断DataView中的记录数是否足够形成多页,
'如果不能,那么就只有一页,且DataGridView需要显示的记录等同于“最后一页”的记录
If _dv.Count <= _RowsPerPage Then
_TotalPage = 1
GoLastPage()
Exit Sub
End If
'可以分为多页的话就要计算总的页数咯,然后DataGridView显示第一页
If _dv.Count Mod _RowsPerPage = 0 Then
_TotalPage = Int(_dv.Count / _RowsPerPage)
Else
_TotalPage = Int(_dv.Count / _RowsPerPage) + 1
End If
GoFirstPage()
End Sub
'到第一页
Public Sub GoFirstPage()
'如果只有一页,那么显示的记录等同于“最后一页”的记录
If _TotalPage = 1 Then
GoLastPage()
Exit Sub
End If
'如果有多页,就到第“1”页
_curPage = 0
GoNoPage(_curPage)
End Sub
Public Sub GoNextPage()
'这段代码主要是为了防止当前页号溢出
_curPage += 1
If _curPage > _TotalPage - 1 Then
_curPage = _TotalPage - 1
Exit Sub
End If
'如果到了最后一页,那就显示最后一页的记录
If _curPage = _TotalPage - 1 Then
GoLastPage()
Exit Sub
End If
'如果没到最后一页,就到指定的“下一页”
GoNoPage(_curPage)
End Sub
Public Sub GoPrevPage()
'防止不合法的当前页号
_curPage -= 1
If _curPage < 0 Then
_curPage = 0
Exit Sub
End If
'到指定的“上一页”
GoNoPage(_curPage)
End Sub
'到最后一页
Public Sub GoLastPage()
_curPage = _TotalPage - 1
Dim i As Integer
Dim dt As New DataTable
'dt只是个临时的DataTable,用来获取所需页数的记录
dt = _dv.ToTable.Clone
For i = (_TotalPage - 1) * _RowsPerPage To _dv.Count - 1
'i值上下限很关键,调试的时候常常这里报错找不到行
'就是因为i值溢出
Dim dr As DataRow = dt.NewRow
dr.ItemArray = _dv.ToTable.Rows(i).ItemArray
dt.Rows.Add(dr)
Next
_DataGridView.DataSource = dt
End Sub
'到指定的页
Public Sub GoNoPage(ByVal PageNo As Integer)
_curPage = PageNo
'防止不合法的页号
If _curPage < 0 Then
MsgBox("页号不能小于1")
Exit Sub
End If
'防止页号溢出
If _curPage >= _TotalPage Then
MsgBox("页号超出上限")
Exit Sub
End If
'如果页号是最后一页,就显示最后一页
If _curPage = _TotalPage - 1 Then
GoLastPage()
Exit Sub
End If
'不是最后一页,那显示指定页号的页
Dim dt As New DataTable
dt = _dv.ToTable.Clone
Dim i As Integer
For i = PageNo * _RowsPerPage To (PageNo + 1) * _RowsPerPage - 1
Dim dr As DataRow = dt.NewRow
dr.ItemArray = _dv.ToTable.Rows(i).ItemArray
dt.Rows.Add(dr)
Next
_DataGridView.DataSource = dt
End Sub
End Class
Private dgvPage As ClsDataGridViewPage
Public Sub New()
' 此调用是 Windows 窗体设计器所必需的。
InitializeComponent()
' 在 InitializeComponent() 调用之后添加任何初始化。
'实例化分页类成员
dgvPage = New ClsDataGridViewPage '分页类成员
End Sub
Private Sub Button3_Click(……)Handles Button3.Click
'略
dataGridView1.DataSource = Nothing
dgvPage.SetDataGridView = Me.dataGridView1
dgvPage.RowSsPerPage = 30'设置每页的行数
dataGridView1.DataSource = BindingSource1
'dataGridView1 并没有每页设置成30行,而是没有分页,这是怎么回事呢?
dgvPage.Paging()
End Sub
Public Sub Paging()
If _dv.Count <= _RowsPerPage Then'这里发生未实例化错误
_TotalPage = 1
GoLastPage()
Exit Sub
End If
If _dv.Count Mod _RowsPerPage = 0 Then
_TotalPage = Int(_dv.Count / _RowsPerPage)
Else
_TotalPage = Int(_dv.Count / _RowsPerPage) + 1
End If
GoFirstPage()
End Sub
Private dgvPage As ClsDataGridViewPage
Public Sub New()
' 此调用是 Windows 窗体设计器所必需的。
InitializeComponent()
' 在 InitializeComponent() 调用之后添加任何初始化。
'实例化分页类成员
dgvPage = New ClsDataGridViewPage '分页类成员
End Sub
Private Sub Button3_Click(……)Handles Button3.Click
'略
dataGridView1.DataSource = Nothing
dgvPage.SetDataGridView = Me.dataGridView1
dgvPage.RowSsPerPage = 30'设置每页的行数
dataGridView1.DataSource = BindingSource1
'dataGridView1 并没有每页设置成30行,而是没有分页,这是怎么回事呢?
End Sub