VB.NET使用DataGridView分页显示

 在使用DataGridView显示来自DataTable的记录时,如果DataTable里的记录太多(上百条),那么用DataGridView查看记录就很不方便,如果能让DataGridView分页显示,每页只显示10条(可以随意规定每页显示的条数),并且配以页面之间的跳转按钮,岂不是方面了许多?现在就来说说我是怎么做到的吧。

我自己编写了一个类用于辅助DataGridView控件的记录分页显示,并提供页面跳转的方法。下面是这个类的源代码:

http://download1.csdn.net/down3/20070610/10102852142.vb

Public Class ClsDataGridViewPage

    '每页记录数
    Private _RowsPerPage As Integer
    '总页数
    Private _TotalPage As Integer
    '当前页数
    Private _curPage As Integer = 0
    '要分页的DataGridView
    Private _DataGridView As Windows.Forms.DataGridView
    '与需要分页显示的的DataView
    Private _dv As DataView

    '获取与设置每页记录数
    Public Property RowsPerPage() 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

现在举例介绍一下怎么使用这个类(以下简称分页辅助类)来帮助DataGridView控件分页显示记录:

首先,在DataGridView所在的窗体类代码中加入该分页辅助类的成员:

Private dgvPage As ClsDataGridViewPage

然后在窗体的构造函数代码中实例化这个成员:

Public Sub New()

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。

        '实例化分页类成员
        dgvPage = New ClsDataGridViewPage '分页类成员
    End Sub

注意:要想让DataGridView分页显示记录,最关键的需要设置的分页类的三个属性是:
SetDataGridView 该属性用于设置窗体上要分页显示记录的DataGridView控件
RowsPerPage 该属性用来设置每页需要显示的记录数
SetDataView 该属性用来设置需要在DataGridView空间上显示的DataView
可以灵活地设置这三个属性,以满足不同的DataGridView对不同的DataView进行指定每页记录数的显示(是不是很方便?)现在举例如何设置这三个属性:

        '设置分页类对象的属性
        dgvPage.GetDataGridView = Me.DataGridView1 '需要分页的是 Me.DataGridView1
        dgvPage.RowsPerPage = 10 '每页显示10条记录

        '获取需要分页显示的DataView
        Dim dt As DataTable = Me.DataGridView1.DataSource
        dgvPage.SetDataView = dt.DefaultView

上述属性设置的过程要在DataGridView第一次设置DataSource时进行。(因为实现分页类实现DataGridView的分页显示是通过改变DataGridView的DataSource属性实现的。当然,可以在任何时候将想要在DataGridView中分页显示的DataView赋给dgvPage.SetDataView ,比如:

dim dv as new DataView=......
dgvPage.SetDataView = dv

这样DataGridView里显示的记录可以动态地变化。)

剩下的事就简单啦:要实现DataGridView的记录分页显示,调用分页类的Paging 方法,
要看第一页,调用分页类的GoFirstPage方法
要看下一页,调用 GoNextPage方法
要看前一页,调用GoPrevPage方法
要看最后一页,调用GoLastPage方法
要看指定页号的页,调用 GoNoPage方法

只要灵活使用好了这个类,你的DataGridView控件的分页功能绝不成问题。而且随时都能了解当前到了第几页(curPage()+1),一共有多少页(TotalPage())

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章