VS2005 自定义控件 Datagridview实现分页的源代码
在论坛得到不少帮助,十分感谢那些无私分享源代码的兄弟,今天在别的论坛看有人求助datagridview
分页就写了一个,现在分享给大家。
首先创建一个类库项目
在自动创建的class1的代码页里复制以下代码,然后生成dll 直接托放到工具栏里使用
源代码 Public Class PageDataGridview
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
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 PageDataGridview_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))
If temptolpage * PageSize >= tol Then
ToltalPage = temptolpage
Else
ToltalPage = temptolpage + 1
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()
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
End If
End Sub
Public Sub ToUp()
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
End If
End Sub
Public Sub ToFirst()
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
End If
End Sub
Public Sub ToLast()
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
End If
End Sub
End Class
本文档为【VS2005 自定义控件 Datagridview实现分页的源代码】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。