Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Inherits System.Windows.Forms.Form
Private da As SqlDataAdapter
Private ds As DataSet
Private dtSource As DataTable
Private pageCount As Integer
Private maxRec As Integer
Private pageSize As Integer
Private currentPage As Integer
Private recNo As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strConn As String = "Data Source=.;Initial Catalog=hbnhzxdata;Integrated Security=True" ' //数据库连接字符串
Dim conn As New SqlConnection
conn = New SqlConnection(strConn)
conn.Open()
Try
da = New SqlDataAdapter("select * from MemberInfo", conn)
ds = New DataSet
da.Fill(ds, "社员信息表")
dtSource = ds.Tables("社员信息表")
conn.Close()
Catch ex As Exception
Throw ex
End Try
Try
pageSize = 5
maxRec = dtSource.Rows.Count
pageCount = maxRec \ pageSize
If (maxRec Mod pageSize) > 0 Then
pageCount = pageCount + 1
End If
currentPage = 1
recNo = 0
LoadPage()
Catch ex As Exception
MessageBox.Show("please insert right number!")
End Try
End Sub
Private Sub btnFristPage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFristPage.Click
If Not CheckFillButton() Then Return
If currentPage = 1 Then
MessageBox.Show("The frist page now!")
Return
End If
currentPage = 1
recNo = 0
LoadPage()
End Sub
Private Sub btnPreviousPage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreviousPage.Click
If Not CheckFillButton() Then Return
If currentPage = pageCount Then
recNo = pageSize * (currentPage - 2)
End If
currentPage -= 1
If currentPage < 1 Then
MessageBox.Show("The frist page now!")
currentPage = 1
Return
Else
recNo = pageSize * (currentPage - 1)
End If
LoadPage()
End Sub
Private Sub btnNextPage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNextPage.Click
If Not CheckFillButton() Then Return
currentPage = currentPage + 1
If currentPage > pageCount Then
currentPage = pageCount
If recNo = maxRec Then
MessageBox.Show("The last page now!")
Return
Else
recNo = pageSize * (currentPage + 1)
End If
End If
LoadPage()
End Sub
Private Sub btnLastPage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLastPage.Click
If Not CheckFillButton() Then Return
If recNo = maxRec Then
MessageBox.Show("The last page now!")
Return
End If
currentPage = pageCount
recNo = pageSize * (currentPage - 1)
LoadPage()
End Sub
Private Sub LoadPage()
Dim i As Integer
Dim startRec As Integer
Dim endRec As Integer
Dim dtTemp As DataTable
Dim dr As DataRow
dtTemp = dtSource.Clone
If currentPage = pageCount Then
endRec = maxRec
Else
endRec = pageSize * currentPage
End If
startRec = recNo
For i = startRec To endRec - 1
dtTemp.ImportRow(dtSource.Rows(i))
recNo = recNo + 1
Next
DataGridView1.DataSource = dtTemp
End Sub
Private Function CheckFillButton() As Boolean
If pageSize = 0 Then
MessageBox.Show("insert the number as one page", "Attention!", MessageBoxButtons.OK, MessageBoxIcon.Warning)
CheckFillButton = False
Else
CheckFillButton = True
End If
End Function