VS2005 自定义控件 Datagridview实现分页的源代码 (VB)

在论坛得到不少帮助,十分感谢那些无私分享源代码的兄弟,今天在别的论坛看有人求助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


PageSize      属性 设置每页显示的数据条数
CurrentPage   属性 返回当前页数
Toltal_Page   属性 返回总页数
ToNext        方法 显示下一页
ToUp          方法  显示上一页
ToFirst       方法 显示第一页
ToLast        显示最后一页
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值