Pager.cs
10.4 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;
namespace WindowsApp.MyControl
{
/// <summary>
/// 申明委托
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
public delegate int EventPagingHandler(EventPagingArg e);
/// <summary>
/// 分页控件呈现
/// </summary>
public partial class Pager : UserControl
{
public Pager()
{
InitializeComponent();
}
public event EventPagingHandler EventPaging;
/// <summary>
/// 每页显示记录数
/// </summary>
private int _pageSize = 20;
/// <summary>
/// 每页显示记录数
/// </summary>
public int PageSize
{
get { return _pageSize; }
set
{
_pageSize = value;
GetPageCount();
}
}
private int _nMax = 0;
/// <summary>
/// 总记录数
/// </summary>
public int NMax
{
get { return _nMax; }
set
{
_nMax = value;
GetPageCount();
}
}
private int _pageCount = 0;
/// <summary>
/// 页数=总记录数/每页显示记录数
/// </summary>
public int PageCount
{
get { return _pageCount; }
set { _pageCount = value; }
}
private int _pageCurrent = 0;
/// <summary>
/// 当前页号
/// </summary>
public int PageCurrent
{
get { return _pageCurrent; }
set { _pageCurrent = value; }
}
public BindingNavigator ToolBar
{
get { return this.bindingNavigator; }
}
private void GetPageCount()
{
if (this.NMax > 0)
{
this.PageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(this.NMax) / Convert.ToDouble(this.PageSize)));
}
else
{
this.PageCount = 0;
}
}
/// <summary>
/// 翻页控件数据绑定的方法
/// </summary>
public void Bind()
{
if (this.EventPaging != null)
{
this.NMax = this.EventPaging(new EventPagingArg(this.PageCurrent));
}
if (this.PageCurrent > this.PageCount)
{
this.PageCurrent = this.PageCount;
}
if (this.PageCount == 1)
{
this.PageCurrent = 1;
}
lblPageCount.Text = this.PageCount.ToString();
this.lblMaxPage.Text = "共"+this.NMax.ToString()+"条记录";
this.txtCurrentPage.Text = this.PageCurrent.ToString();
if (this.PageCurrent == 1)
{
this.btnPrev.Enabled = false;
this.btnFirst.Enabled = false;
}
else
{
btnPrev.Enabled = true;
btnFirst.Enabled = true;
}
if (this.PageCurrent == this.PageCount)
{
this.btnLast.Enabled = false;
this.btnNext.Enabled = false;
}
else
{
btnLast.Enabled = true;
btnNext.Enabled = true;
}
if (this.NMax == 0)
{
btnNext.Enabled = false;
btnLast.Enabled = false;
btnFirst.Enabled = false;
btnPrev.Enabled = false;
}
}
private void btnFirst_Click(object sender, EventArgs e)
{
PageCurrent = 1;
this.Bind();
}
private void btnPrev_Click(object sender, EventArgs e)
{
PageCurrent -= 1;
if (PageCurrent <= 0)
{
PageCurrent = 1;
}
this.Bind();
}
private void btnNext_Click(object sender, EventArgs e)
{
this.PageCurrent += 1;
if (PageCurrent > PageCount)
{
PageCurrent = PageCount;
}
this.Bind();
}
private void btnLast_Click(object sender, EventArgs e)
{
PageCurrent = PageCount;
this.Bind();
}
private void btnGo_Click(object sender, EventArgs e)
{
if (this.txtCurrentPage.Text != null && txtCurrentPage.Text != "")
{
if (Int32.TryParse(txtCurrentPage.Text, out _pageCurrent))
{
this.Bind();
}
else
{
MessageBox.Show("输入数字格式错误!");
}
}
}
private void txtCurrentPage_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
this.Bind();
}
}
}
/// <summary>
/// 自定义事件数据基类
/// </summary>
public class EventPagingArg : EventArgs
{
private int _intPageIndex;
public EventPagingArg(int PageIndex)
{
_intPageIndex = PageIndex;
}
}
/// <summary>
/// 数据源提供
/// </summary>
public class PageData
{
private int _PageSize = 10;
private int _PageIndex = 1;
private int _PageCount = 0;
private int _TotalCount = 0;
private string _TableName;//表名
private string _QueryFieldName = "*";//表字段FieldStr
private string _OrderStr = string.Empty; //排序_SortStr
private string _QueryCondition = string.Empty;//查询的条件 RowFilter
private string _PrimaryKey = string.Empty;//主键
private bool _isQueryTotalCounts = true;//是否查询总的记录条数
/// <summary>
/// 是否查询总的记录条数
/// </summary>
public bool IsQueryTotalCounts
{
get { return _isQueryTotalCounts; }
set { _isQueryTotalCounts = value; }
}
/// <summary>
/// 显示页数
/// </summary>
public int PageSize
{
get
{
return _PageSize;
}
set
{
_PageSize = value;
}
}
/// <summary>
/// 当前页
/// </summary>
public int PageIndex
{
get
{
return _PageIndex;
}
set
{
_PageIndex = value;
}
}
/// <summary>
/// 总页数
/// </summary>
public int PageCount
{
get
{
return _PageCount;
}
}
/// <summary>
/// 总记录数
/// </summary>
public int TotalCount
{
get
{
return _TotalCount;
}
}
/// <summary>
/// 表名,包括视图
/// </summary>
public string TableName
{
get
{
return _TableName;
}
set
{
_TableName = value;
}
}
/// <summary>
/// 表字段FieldStr
/// </summary>
public string QueryFieldName
{
get
{
return _QueryFieldName;
}
set
{
_QueryFieldName = value;
}
}
/// <summary>
/// 排序字段
/// </summary>
public string OrderStr
{
get
{
return _OrderStr;
}
set
{
_OrderStr = value;
}
}
/// <summary>
/// 查询条件
/// </summary>
public string QueryCondition
{
get
{
return _QueryCondition;
}
set
{
_QueryCondition = value;
}
}
/// <summary>
/// 主键
/// </summary>
public string PrimaryKey
{
get {
return _PrimaryKey;
}
set {
_PrimaryKey = value;
}
}
public DataSet QueryDataTable(String where)
{
SqlParameter[] parameters = {
new SqlParameter("@Tables", SqlDbType.VarChar, 255),
new SqlParameter("@PrimaryKey" , SqlDbType.VarChar , 255),
new SqlParameter("@Sort", SqlDbType.VarChar , 255 ),
new SqlParameter("@CurrentPage", SqlDbType.Int),
new SqlParameter("@PageSize", SqlDbType.Int),
new SqlParameter("@Fields", SqlDbType.VarChar, 255),
new SqlParameter("@Filter", SqlDbType.VarChar,1000),
new SqlParameter("@Group" ,SqlDbType.VarChar , 1000 )
};
parameters[0].Value = _TableName;
parameters[1].Value = _PrimaryKey;
parameters[2].Value = _OrderStr;
parameters[3].Value = PageIndex;
parameters[4].Value = PageSize;
parameters[5].Value =_QueryFieldName;
parameters[6].Value = _QueryCondition;
parameters[7].Value = string.Empty;
//DataSet ds = DbHelperSQL.RunProcedure("SP_Pagination", parameters, "dd");
//if (_isQueryTotalCounts)
//{
// _TotalCount = GetTotalCount();
//}
//if (_TotalCount == 0)
//{
// _PageIndex = 0;
// _PageCount = 0;
//}
//else
//{
// _PageCount = _TotalCount % _PageSize == 0 ? _TotalCount / _PageSize : _TotalCount / _PageSize + 1;
// if (_PageIndex > _PageCount)
// {
// _PageIndex = _PageCount;
// parameters[4].Value = _PageSize;
// ds = QueryDataTable("");
// }
//}
return null;
}
public int GetTotalCount()
{
string strSql = " select count(1) from "+_TableName;
if (_QueryCondition != string.Empty)
{
strSql +=" where " + _QueryCondition;
}
return 0;
}
}
}