实现分页控件的代码非常的简单,下面直接给出全部源码,大家可以参考下,整个分页控件的源码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace RDIFramework.Controls
{ public delegate void PageChangedEventHandler(object sender, EventArgs e);
/// <summary>
/// 分页用户控件,仅提供分页信息显示及改变页码操作
/// </summary>
public partial class UcPagerEx : UserControl
{ public event PageChangedEventHandler PageChanged;
private int _pageSize;
private int m_PageCount;
private int _recordCount;
private int _pageIndex;
public UcPagerEx()
{
InitializeComponent();
this._pageSize = 10;
this._recordCount = 0;
this._pageIndex = 1; //默认为第一页
}
/// <summary>
/// 带参数的构造函数
/// <param name="pageSize">每页记录数</param>
/// <param name="recordCount">总记录数</param>
/// </summary>
public UcPagerEx(int recordCount, int pageSize)
{
InitializeComponent();
this._pageSize = pageSize;
this._recordCount = recordCount;
this._pageIndex = 1; //默认为第一页
this.InitPageInfo();
}
protected virtual void OnPageChanged(EventArgs e)
{
if (PageChanged != null)
{
InitPageInfo();
PageChanged(this, e);
}
}
[Description("设置或获取一页中显示的记录数目"), DefaultValue(20), Category("分页")]
public int PageSize
{
set
{
this._pageSize = value;
}
get
{
return this._pageSize;
}
}
[Description("获取记录总页数"), DefaultValue(0), Category("分页")]
public int PageCount
{
get
{
return this.m_PageCount;
}
}
[Description("设置或获取记录总数"), Category("分页")]
public int RecordCount
{
set
{
this._recordCount = value;
}
get
{
return this._recordCount;
}
}
[Description("当前的页面索引, 开始为1"), DefaultValue(0), Category("分页")]
[Browsable(false)]
public int PageIndex
{
set
{
this._pageIndex = value;
}
get
{
return this._pageIndex;
}
}
/// <summary>
/// 初始化分页信息
/// <param name="pageSize">每页记录数</param>
/// <param name="recordCount">总记录数</param>
/// </summary>
public void InitPageInfo(int recordCount, int pageSize)
{
this._recordCount = recordCount;
this._pageSize = pageSize;
this.InitPageInfo();
}
/// <summary>
/// 初始化分页信息
/// <param name="recordCount">总记录数</param>
/// </summary>
public void InitPageInfo(int recordCount)
{
this._recordCount = recordCount;
this.InitPageInfo();
}
/// <summary>
/// 初始化分页信息
/// </summary>
public void InitPageInfo()
{
if (this._pageSize < 1)
this._pageSize = 10; //如果每页记录数不正确,即更改为10
if (this._recordCount < 0)
this._recordCount = 0; //如果记录总数不正确,即更改为0
//取得总页数
if (this._recordCount % this._pageSize == 0)
{
this.m_PageCount = this._recordCount / this._pageSize;
}
else
{
this.m_PageCount = this._recordCount / this._pageSize + 1;
}
//设置当前页
if (this._pageIndex > this.m_PageCount)
{
this._pageIndex = this.m_PageCount;
}
if (this._pageIndex < 1)
{
this._pageIndex = 1;
}
//设置上一页按钮的可用性
bool enable = (this.PageIndex > 1);
this.btnPrevious.Enabled = enable;
//设置首页按钮的可用性
enable = (this.PageIndex > 1);
this.btnFirst.Enabled = enable;
//设置下一页按钮的可用性
enable = (this.PageIndex < this.PageCount);
this.btnNext.Enabled = enable;
//设置末页按钮的可用性
enable = (this.PageIndex < this.PageCount);
this.btnLast.Enabled = enable;
this.txtPageIndex.Text = this._pageIndex.ToString();
this.lblPageInfo.Text = string.Format("共 {0} 条记录,每页 {1} 条,共 {2} 页", this._recordCount, this._pageSize, this.m_PageCount);
}
public void RefreshData(int page)
{
this._pageIndex = page;
EventArgs e = new EventArgs();
OnPageChanged(e);
}
private void btnFirst_Click(object sender, System.EventArgs e)
{
this.RefreshData(1);
}
private void btnPrevious_Click(object sender, System.EventArgs e)
{
if (this._pageIndex > 1)
{
this.RefreshData(this._pageIndex - 1);
}
else
{
this.RefreshData(1);
}
}
private void btnNext_Click(object sender, System.EventArgs e)
{
if (this._pageIndex < this.m_PageCount)
{
this.RefreshData(this._pageIndex + 1);
}
else if (this.m_PageCount < 1)
{
this.RefreshData(1);
}
else
{
this.RefreshData(this.m_PageCount);
}
}
private void btnLast_Click(object sender, System.EventArgs e)
{
this.RefreshData(this.m_PageCount > 0 ? this.m_PageCount : 1);
}
private void txtPageIndex_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
int num;
try
{
num = Convert.ToInt16(this.txtPageIndex.Text);
}
catch
{
num = 1;
}
if (num > this.m_PageCount)
num = this.m_PageCount;
if (num < 1)
num = 1;
this.RefreshData(num);
}
}
}
} |
代码基本没有什么难度,相信大家都能看得懂,那么如何使用这个控件呢?
首先在生成的工具箱中拖动这个分页控件到界面上,再后再做数据绑定的代码即可。我直接展示一个已经做成的界面,如下图所示:
上面就是分页的效果,如何实现的呢?下面给出实现代码。
我们可以在Load事件中调用下面的Search()方法对数据进行绑定,如下代码所示:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
private void Search()
{ var recordCount = 0;
this.DTProductInfo = GetData(out recordCount, ucPager.PageIndex, ucPager.PageSize, this.searchValue);
ucPager.RecordCount = recordCount;
ucPager.InitPageInfo();
// 加载绑定数据
this.GetList();
}private DataTable GetData(out int recordCount, int pageIndex, int pageSize,string search)
{ return new ProductInfoManager(dbProvider).GetDTByPage(out recordCount, pageIndex, pageSize, search,ProductInfoTable.FieldCreateOn + " DESC ");
}public override void GetList()
{ this.dgvProductInfo.AutoGenerateColumns = false;
if (this.DTProductInfo.Columns.Count > 0)
{
this.DTProductInfo.DefaultView.Sort = ProductInfoTable.FieldCreateOn;
}
this.dgvProductInfo.DataSource = this.DTProductInfo.DefaultView;
this.SetControlState();
} |
同时需要对UcPagerEx的PageChanged事件做处理,以启用用户分页的需求,代码如下:
|
1
2
3
4
5
6
7
|
private void ucPager_PageChanged(object sender, EventArgs e)
{ var holdCursor = this.Cursor;
this.Cursor = Cursors.WaitCursor;
Search();
this.Cursor = holdCursor;
} |
附注:对于上面的“GetDTByPage”方法可以任意实现,可以调用存储过程,也可以使用代码进行分页。只要返回分页的数据即可。
下面给出一些分页的效果,如下图所示:
本文转自yonghu86 51CTO博客,原文链接:http://blog.51cto.com/yonghu/1599021,如需转载请自行联系原作者