以前都是从事b/s开发,由于公司有个比较大的c/s项目,在使用datagridview的时候,显示数据量比较大,所以才用分页模式,也不知道这样是否正确。
想找个c/s下面的分页控件,都没有什么好的,就自己跟b/s下的分页控件,修改成winform下面的。
首先创建一个用户控件名称为pager,在控件中拖入bindingnavigator和bindingsource,修改bindingnavigator,加入必要的一些控件。
效果如下:
代码实现如下:
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
|
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; } } 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 { common.messageprocess.showerror( "输入数字格式错误!" ); } } } } /**/ /// <summary> /// 自定义事件数据基类 /// </summary> public class eventpagingarg : eventargs { private int _intpageindex; public eventpagingarg( int pageindex) { _intpageindex = pageindex; } } } |
控件功能基本实现。
如何绑定数据呢?
大数量分页,使用存储过程。
这个存储过程是网络上考的,呵呵。我把它给贴出来,希望原作者别砸我砖头。
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
|
alter procedure sp_pagination /**//* *************************************************************** ** 千万数量级分页存储过程 ** *************************************************************** 参数说明: 1.tables :表名称,视图 2.primarykey :主关键字 3.sort :排序语句,不带 order by 比如:newsid desc ,orderrows asc 4.currentpage :当前页码 5.pagesize :分页尺寸 6.filter :过滤语句,不带 where 7. group : group 语句,不带 group by 效果演示:http://www.cn5135.com/_app/enterprise/queryresult.aspx ***************************************************************/ ( @tables varchar (2000), @primarykey varchar (500), @sort varchar (500) = null , @currentpage int = 1, @pagesize int = 10, @fields varchar (2000) = '*' , @filter varchar (1000) = null , @ group varchar (1000) = null ) as /**//*默认排序*/ if @sort is null or @sort = '' set @sort = @primarykey declare @sorttable varchar (1000) declare @sortname varchar (1000) declare @strsortcolumn varchar (1000) declare @operator char (2) declare @type varchar (1000) declare @prec int /**//*设定排序语句.*/ if charindex( 'desc' ,@sort)>0 begin set @strsortcolumn = replace (@sort, 'desc' , '' ) set @operator = '<=' end else begin if charindex( 'asc' , @sort) = 0 set @strsortcolumn = replace (@sort, 'asc' , '' ) set @operator = '>=' end if charindex( '.' , @strsortcolumn) > 0 begin set @sorttable = substring (@strsortcolumn, 0, charindex( '.' ,@strsortcolumn)) set @sortname = substring (@strsortcolumn, charindex( '.' ,@strsortcolumn) + 1, len(@strsortcolumn)) end else begin set @sorttable = @tables set @sortname = @strsortcolumn end select @type=t. name , @prec=c.prec from sysobjects o join syscolumns c on o.id=c.id join systypes t on c.xusertype=t.xusertype where o. name = @sorttable and c. name = @sortname if charindex( 'char' , @type) > 0 set @type = @type + '(' + cast (@prec as varchar ) + ')' declare @strpagesize varchar (500) declare @strstartrow varchar (500) declare @strfilter varchar (1000) declare @strsimplefilter varchar (1000) declare @strgroup varchar (1000) /**//*默认当前页*/ if @currentpage < 1 set @currentpage = 1 /**//*设置分页参数.*/ set @strpagesize = cast (@pagesize as varchar (500)) set @strstartrow = cast (((@currentpage - 1)*@pagesize + 1) as varchar (500)) /**//*筛选以及分组语句.*/ if @filter is not null and @filter != '' begin set @strfilter = ' where ' + @filter + ' ' set @strsimplefilter = ' and ' + @filter + ' ' end else begin set @strsimplefilter = '' set @strfilter = '' end if @ group is not null and @ group != '' set @strgroup = ' group by ' + @ group + ' ' else set @strgroup = '' /**//*执行查询语句*/ exec ( ' declare @sortcolumn ' + @type + ' set rowcount ' + @strstartrow + ' select @sortcolumn=' + @strsortcolumn + ' from ' + @tables + @strfilter + ' ' + @strgroup + ' order by ' + @sort + ' set rowcount ' + @strpagesize + ' select ' + @fields + ' from ' + @tables + ' where ' + @strsortcolumn + @operator + ' @sortcolumn ' + @strsimplefilter + ' ' + @strgroup + ' order by ' + @sort + ' ' ) |
使用该存储过陈,得到数据,将数据绑定到数据控件,提供了一个pagedata类
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
|
/**/ /// <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; //主键 /**/ /// <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() { 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" ); _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 ds; } public int gettotalcount() { string strsql = " select count(1) from " +_tablename; if (_querycondition != string .empty) { strsql += " where " + _querycondition; } return int .parse(dbhelpersql.getsingle(strsql).tostring()); } } |
好了,在页面放个datagridview 拖入控件pager
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 receiveorderjlform_load( object sender, eventargs e) { this .pager1.pagecurrent = 1; this .pager1.bind(); } private int dgvbind() { windowsapp.mycontrol.pagedata pagedata = new windowsapp.mycontrol.pagedata(); pagedata.tablename = "t_receiveorder" ; pagedata.primarykey = "receiveorderid" ; pagedata.orderstr = "receiveorderid desc" ; pagedata.pageindex = this .pager1.pagecurrent; pagedata.pagesize = this .pager1.pagesize; pagedata.querycondition = _strsql + strwhere.tostring(); pagedata.queryfieldname = "*" ; this .pager1.bindingsource.datasource = pagedata.querydatatable().tables[0]; this .pager1.bindingnavigator.bindingsource = pager1.bindingsource; dgvreceiveorder.autogeneratecolumns = false ; dgvreceiveorder.datasource = this .pager1.bindingsource; return pagedata.totalcount; } private int pager1_eventpaging(windowsapp.mycontrol.eventpagingarg e) { return dgvbind(); } |
效果如下
源码下载:winformpager.rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。