“VB.Net使用datagridview实现分页”我不会用,高手指点

lkhoji 2010-08-09 08:29:44
找到了一个VB.Net使用datagridview实现分页(点击查看原文)
看了半天了也不会用。
没搞明白这个类为什么没有继承datagridview。也许什么都没搞明白,请高手指点,谢谢!

Public Class ClsDataGridViewPage

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

Public Property RowSsPerPage() 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())
...全文
1109 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangcced 2012-03-25
  • 打赏
  • 举报
回复
数据超10万条, 翻页超慢
lkhoji 2010-08-10
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 asdfy 的回复:]
dv.Rowfilter
[/Quote]
这个可以,不过还没用过这种方法。还不知道表达式是什么样,dv.Rowfilter之后DataGridView1会自动刷新吗 。如果这些和BindingSource的不一样,程序就要改动会大些。
古今多少事 2010-08-10
  • 打赏
  • 举报
回复
dv.Rowfilter
lkhoji 2010-08-10
  • 打赏
  • 举报
回复
dv没有这个方法
古今多少事 2010-08-10
  • 打赏
  • 举报
回复
dv.filter?
lkhoji 2010-08-10
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 asdfy 的回复:]
Dim dv As DataView = xxDataSet.Tables(0).DefaultView
换成这样。不知道你这样绑定数据的行不。
[/Quote]
ok!
可是这样的话,我不知道怎么筛选了,请给个思路
古今多少事 2010-08-09
  • 打赏
  • 举报
回复
Dim dv As DataView = xxDataSet.Tables(0).DefaultView
换成这样。不知道你这样绑定数据的行不。
lkhoji 2010-08-09
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 asdfy 的回复:]
Dim dgvPage As New ClsDataGridViewPage
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dgvPage.GetDataGridView = Me.DataGridView1
dgvPage……
[/Quote]
这句代码BindingSource1.dataview出现错误“dataview”不是“System.Windows.Forms.BindingSource”的成员。
古今多少事 2010-08-09
  • 打赏
  • 举报
回复
Dim dgvPage As New ClsDataGridViewPage
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dgvPage.GetDataGridView = Me.DataGridView1
dgvPage.RowSPerPage = 30
dim dv as dataview=bindingsource1.dataview
dgvPage.SetDataView = dv
dgvPage.Paging()
End Sub

这样试试。
threenewbee 2010-08-09
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lkhoji 的回复:]
引用 1 楼 caozhy 的回复:
它是一个helper类。也就是传递你需要操作的datagridview进去,即可。
初始化:
Private dgvp As New ClsDataGridViewPage(Me.dataGridView1)
使用
dgvp.XXXX()

Private dgvp As New ClsDataGridViewPage(Me.dataGridVi……
[/Quote]
Public Sub New(ByVal datagridview As Windows.Forms.DataGridView, ByVal dv As DataView, ByVal RowsPerPage As Integer)
应该传3个参数,而不是1个。
我给你的是原则性的代码。
Private dgvp As New ClsDataGridViewPage(Me.dataGridView1, dv, 10) '第二个参数指定数据源,第3个参数表示每页数量。
lkhoji 2010-08-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 asdfy 的回复:]
调用分页类的Paging 方法
[/Quote]
我对类的理解式中是很朦胧的,没理解透,只能通过实践渐渐理解吧。
调用分页类的Paging 方法后,使用下面代码,在ClsDataGridViewPage 类中出现未实例化的错误
    Private dgvPage As ClsDataGridViewPage
Public Sub New()
' 此调用是 Windows 窗体设计器所必需的。
InitializeComponent()
' 在 InitializeComponent() 调用之后添加任何初始化。
'实例化分页类成员
dgvPage = New ClsDataGridViewPage '分页类成员
End Sub

Private Sub Button3_Click(……)Handles Button3.Click
'略
dataGridView1.DataSource = Nothing
dgvPage.SetDataGridView = Me.dataGridView1
dgvPage.RowSsPerPage = 30'设置每页的行数
dataGridView1.DataSource = BindingSource1
'dataGridView1 并没有每页设置成30行,而是没有分页,这是怎么回事呢?
dgvPage.Paging()
End Sub

下面是错误发生的位置。
    Public Sub Paging()
If _dv.Count <= _RowsPerPage Then'这里发生未实例化错误
_TotalPage = 1
GoLastPage()
Exit Sub
End If
If _dv.Count Mod _RowsPerPage = 0 Then
_TotalPage = Int(_dv.Count / _RowsPerPage)
Else
_TotalPage = Int(_dv.Count / _RowsPerPage) + 1
End If
GoFirstPage()
End Sub
古今多少事 2010-08-09
  • 打赏
  • 举报
回复
调用分页类的Paging 方法
lkhoji 2010-08-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 caozhy 的回复:]
它是一个helper类。也就是传递你需要操作的datagridview进去,即可。
初始化:
Private dgvp As New ClsDataGridViewPage(Me.dataGridView1)
使用
dgvp.XXXX()
[/Quote]
Private dgvp As New ClsDataGridViewPage(Me.dataGridView1)
上面的样的生命产生下面错误
错误:没有可访问的“New”接受此数目的参数,因此重载决策失败。

按原作者的说明,用如下关键代码
    Private dgvPage As ClsDataGridViewPage
Public Sub New()
' 此调用是 Windows 窗体设计器所必需的。
InitializeComponent()
' 在 InitializeComponent() 调用之后添加任何初始化。
'实例化分页类成员
dgvPage = New ClsDataGridViewPage '分页类成员
End Sub

Private Sub Button3_Click(……)Handles Button3.Click
'略
dataGridView1.DataSource = Nothing
dgvPage.SetDataGridView = Me.dataGridView1
dgvPage.RowSsPerPage = 30'设置每页的行数
dataGridView1.DataSource = BindingSource1
'dataGridView1 并没有每页设置成30行,而是没有分页,这是怎么回事呢?
End Sub
古今多少事 2010-08-09
  • 打赏
  • 举报
回复
说得很明白了,原文有几处手误的地方,改成下面:
Public Property RowSPerPage() As Integer '获取与设置每页记录数
Get
'……
End Property

'设置需要分页的GetDataGridView
Public WriteOnly Property GetDataGridView()
Set(ByVal value As Object)
_DataGridView = value
End Set
End Property

threenewbee 2010-08-09
  • 打赏
  • 举报
回复
它是一个helper类。也就是传递你需要操作的datagridview进去,即可。
初始化:
Private dgvp As New ClsDataGridViewPage(Me.dataGridView1)
使用
dgvp.XXXX()
vb.net操作DataGridView控件的用法的集合,包括: 1. DataGridView当前的单元格属性取得、变更 2. DataGridView编辑属性 3. DataGridView最下面一列新追加行非表示 4. DataGridView判断当前选中行是否为新追加的行 5. DataGridView删除行可否设定 6. DataGridView行列不表示和删除 DataGridView控件用法合集(二) 7. DataGridView行列宽度高度设置为不能编辑 8. DataGridView行高列幅自动调整 9. DataGridView指定行列冻结 10. DataGridView列顺序变更可否设定 11. DataGridView行复数选择 12. DataGridView选择的行、列、单元格取得 DataGridView控件用法合集(三) 13. DataGridView指定单元格是否表示 14. DataGridView表头部单元格取得 15. DataGridView表头部单元格文字列设定 16. DataGridView选择的部分拷贝至剪贴板 17.DataGridView粘贴 18. DataGridView单元格上ToolTip表示设定(鼠标移动到相应单元格上时,弹出说明信息) DataGridView控件用法合集(四) 19. DataGridView中的ContextMenuStrip属性 20. DataGridView指定滚动框位置 21. DataGridView手动追加列 22. DataGridView全体分界线样式设置 23. DataGridView根据单元格属性更改显示内容 24. DataGridView新追加行的行高样式设置る 25. DataGridView新追加行单元格默认值设置 DataGridView中输入错误数据的处理(五) 26. DataGridView单元格数据错误标签表示 27. DataGridView单元格内输入值正确性判断 28. DataGridView单元格输入错误值事件的捕获 DataGridView控件用法合集(六) 29. DataGridView行排序(点击列表头自动排序的设置) 30. DataGridView自动行排序(新追加值也会自动排序) 31. DataGridView自动行排序禁止情况下的排序 32. DataGridView指定列指定排序 DataGridView控件用法合集(七) 33. DataGridView单元格样式设置 34. DataGridView文字表示位置的设定 35. DataGridView单元格内文字列换行 36. DataGridView单元格DBNull值表示的设定 37. DataGridView单元格样式格式化 38. DataGridView指定单元格颜色设定 39. DataGridView单元格文字字体设置 40. DataGridView根据单元格值设定单元格样式 DataGridView控件用法合集(八) 41. DataGridView设置单元格背景颜色 42. DataGridView行样式描画 43. DataGridView显示行号 44. DataGridView焦点所在单元格焦点框不显示的设定 DataGridView控件用法合集(九) 45. DataGridView中显示选择框CheckBox 46. DataGridView中显示下拉框ComboBox 47. DataGridView单击打开下拉框 48. DataGridView中显示按钮 49. DataGridView中显示链接 50. DataGridView中显示图像 DataGridView控件用法合集(十) 51. DataGridView编辑中单元格控件取得 52. DataGridView输入自动完成 53. DataGridView单元格编辑时键盘KEY事件取得 54. DataGridView下拉框(ComboBox)单元格编辑时事件取得 55. DataGridView下拉框(ComboBox)单元格允许文字输入设定 DataGridView控件用法合集(十一) 56. DataGridView根据值不同在另一列中显示相应图片 57. DataGridView中显示进度条(ProgressBar) 58. DataGridView中添加MaskedTextBox DataGridView控件用法合集(十二) 59. DataGridView中Enter键按下焦点移至旁边的单元格 60. DataGridView行集合化(Group)

16,547

社区成员

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

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