vb.net datagridview 怎么控制分页

zq123zz 2010-12-24 12:17:12
在form窗体中我该怎么分页谢谢了。


Imports System.Windows.Forms
Public Class dvgfy
#Region "DataGridview分页操作说明"
'PageSize 属性 设置每页显示的数据条数
'CurrentPage 属性 返回当前页数
'Toltal_Page 属性 返回总页数
'ToNext 方法 显示下一页
'ToUp 方法 显示上一页
'ToFirst 方法 显示第一页
'ToLast 显示最后一页
'CountSize 获得记录总数
#End Region
Inherits System.Windows.Forms.DataGridView
Private ds As New DataSet
Private ToltalPage As Integer = 1
Private NowPage As Integer = 1
Private OnePageRow As Integer
Private nowrowno As Integer = 0
Private CountSize As Integer '自己加的 为了获得总记录
Public ReadOnly Property Count_Size() As Integer '自己加的
Get
Return CountSize
End Get
End Property '自己加的
Public Property PageSize() As Integer
Get
Return OnePageRow
End Get
Set(ByVal value As Integer)
If value < 0 Then
Exit Property
End If
OnePageRow = value
End Set
End Property
Public ReadOnly Property CurrentPage() As Integer
Get
Return NowPage
End Get
End Property
Public ReadOnly Property Toltal_Page() As Integer
Get
Return ToltalPage
End Get
End Property
Private Sub dvgfy_DataSourceChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.DataSourceChanged
Dim Datagrid As DataGridView = CType(sender, DataGridView)
If CType(Datagrid.DataSource, DataTable).Rows.Count > 0 Then
ToltalPage = 1
NowPage = 1
If PageSize = 0 Then
Exit Sub
End If
Dim table As DataTable = CType(Datagrid.DataSource, DataTable)
Dim tol As Integer = table.Rows.Count
Dim temptolpage As Integer = CInt(Int(tol / PageSize))
CountSize = tol '自己加的 获取总记录数
If temptolpage * PageSize >= tol Then
ToltalPage = temptolpage
Else
ToltalPage = temptolpage + 1
End If
If CountSize <= 4 Then
'If CountSize <= PageSize Then '判断记录数是否小于每页需要显示的数量,如果是的话就将每页显示的数量改变为记录的数量
PageSize = CountSize
ToltalPage = 1
'End If
End If
nowrowno = 0
ds.Tables.Clear()
For tbcount As Integer = 0 To ToltalPage - 1
Dim temptable As New DataTable
For i As Integer = 0 To CType(Datagrid.DataSource, DataTable).Columns.Count - 1
Dim col As New DataColumn
col.ColumnName = CType(Datagrid.DataSource, DataTable).Columns(i).ColumnName
temptable.Columns.Add(col)
Next
For s As Integer = 1 To PageSize
If nowrowno = CType(Datagrid.DataSource, DataTable).Rows.Count Then
Exit For
End If
temptable.ImportRow(CType(Datagrid.DataSource, DataTable).Rows(nowrowno))
nowrowno += 1
Next
ds.Tables.Add(temptable)
Next
If ds.Tables.Count > 0 Then
CType(Datagrid.DataSource, DataTable).Rows.Clear()
For n As Integer = 0 To PageSize - 1
CType(Datagrid.DataSource, DataTable).ImportRow(ds.Tables(0).Rows(n))
Next
End If
End If
End Sub
Public Sub ToNext(ByVal For_Focus)
If Me.DataSource Is Nothing Then
Exit Sub
End If
If NowPage = ToltalPage Then
MsgBox("已经到最后一页")
Else
CType(Me.DataSource, DataTable).Rows.Clear()
For i As Integer = 0 To ds.Tables(NowPage).Rows.Count - 1
CType(Me.DataSource, DataTable).ImportRow(ds.Tables(NowPage).Rows(i))
Next
NowPage += 1
For_Focus.Focus() '使 For_Focus 获得焦点
End If
End Sub
Public Sub ToUp(ByVal For_Focus)
If Me.DataSource Is Nothing Then
Exit Sub
End If
If NowPage = 1 Then
MsgBox("已经到第一页")
Else
CType(Me.DataSource, DataTable).Rows.Clear()
For i As Integer = 0 To ds.Tables(NowPage - 2).Rows.Count - 1
CType(Me.DataSource, DataTable).ImportRow(ds.Tables(NowPage - 2).Rows(i))
Next
NowPage -= 1
For_Focus.Focus() '使 For_Focus 获得焦点
End If

End Sub
Public Sub ToFirst(ByVal For_Focus)
If Me.DataSource Is Nothing Then
Exit Sub
End If
If NowPage = 1 Then
MsgBox("已经到第一页")
Else
CType(Me.DataSource, DataTable).Rows.Clear()
For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
CType(Me.DataSource, DataTable).ImportRow(ds.Tables(0).Rows(i))
Next
NowPage = 1
For_Focus.Focus() '使 For_Focus 获得焦点
End If
End Sub
Public Sub ToLast(ByVal For_Focus)
If Me.DataSource Is Nothing Then
Exit Sub
End If
If NowPage = ToltalPage Then
MsgBox("已经到最后一页")
Else
CType(Me.DataSource, DataTable).Rows.Clear()
For i As Integer = 0 To ds.Tables(ToltalPage - 1).Rows.Count - 1
CType(Me.DataSource, DataTable).ImportRow(ds.Tables(ToltalPage - 1).Rows(i))
Next
NowPage = ToltalPage
For_Focus.Focus() '使 For_Focus 获得焦点
End If
End Sub
Public Sub ToPage(ByVal For_Pages As Object, ByVal For_Focus As Object, ByVal PageNum As Integer)
If PageNum = NowPage Or Me.DataSource Is Nothing Then '当提交的页数=当前页数的话就不执行;无记录不执行。
Exit Sub
End If
If PageNum > ToltalPage Or PageNum < 1 Or PageNum = NowPage Then
MsgBox("最小页数为1" & (Chr(13)) & "最大页数为" & ToltalPage, , "提示")
NowPage = CurrentPage '获取当前页数
For_Pages.text = NowPage
For_Pages.SelectAll() '访问的页数出错之后将文本框的数字全选,方便操作
Exit Sub
Else
CType(Me.DataSource, DataTable).Rows.Clear()
For i As Integer = 0 To ds.Tables(PageNum - 1).Rows.Count - 1
CType(Me.DataSource, DataTable).ImportRow(ds.Tables(PageNum - 1).Rows(i))
Next
NowPage = PageNum '获取当前页数
For_Focus.Focus() '使 For_Focus 获得焦点
End If
End Sub
End Class
...全文
243 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zq123zz 2010-12-26
  • 打赏
  • 举报
回复
就是在FORM窗体中有个上一页下一页按钮怎么点下一页的时候显示下一页的内容,
colorall 2010-12-25
  • 打赏
  • 举报
回复
怎么分,你的代码太多了没仔细看,大概就是这么分的

16,547

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧