本例子使用自定義控件方法實現(xiàn),數(shù)據(jù)庫使用的是SQL Server,實現(xiàn)過程如下:
1、新建一個自定義控件,命名為:PageControl。
2、PageControl代碼如下:
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
|
public partial class PageControl : UserControl { //委托及事件 public delegate void BindPage( int pageSize, int pageIndex, out int totalCount); public event BindPage BindPageEvent; //屬性 public int PageSize { get; set; } = 1 ; //每頁顯示記錄數(shù) public int PageIndex { get; set; } //頁序號 public int TotalCount { get; set; } //總記錄數(shù) public int PageCount { get; set; } //總頁數(shù) public PageControl() { InitializeComponent(); //取消下劃線 linkFirst.LinkBehavior = LinkBehavior.NeverUnderline; linkPrev.LinkBehavior = LinkBehavior.NeverUnderline; linkNext.LinkBehavior = LinkBehavior.NeverUnderline; linkLast.LinkBehavior = LinkBehavior.NeverUnderline; linkGo.LinkBehavior = LinkBehavior.NeverUnderline; } /// <summary> /// 設(shè)置頁 /// </summary> public void SetPage() { //總記錄數(shù) int totalCount = 0 ; BindPageEvent(PageSize, PageIndex + 1 , out totalCount); TotalCount = totalCount; //總頁數(shù) if (TotalCount % PageSize == 0 ) PageCount = TotalCount / PageSize; else PageCount = TotalCount / PageSize + 1 ; //當(dāng)前頁及總頁數(shù) txtCurrentPage.Text = (PageIndex + 1 ).ToString(); lblTotalPage.Text = "共 " + PageCount.ToString() + " 頁" ; } /// <summary> /// 首頁 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void linkFirst_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { if (e.Button == MouseButtons.Left) { PageIndex = 0 ; SetPage(); } } /// <summary> /// 上一頁 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void linkPrve_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { if (e.Button == MouseButtons.Left) { PageIndex--; if (PageIndex < 0 ) { PageIndex = 0 ; } SetPage(); } } /// <summary> /// 下一頁 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void linkNext_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { if (e.Button == MouseButtons.Left) { PageIndex++; if (PageIndex > PageCount - 1 ) { PageIndex = PageCount - 1 ; } SetPage(); } } /// <summary> /// 末頁 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void linkLast_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { if (e.Button == MouseButtons.Left) { PageIndex = PageCount - 1 ; SetPage(); } } /// <summary> /// 只能按0-9、Delete、Enter、Backspace鍵 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void txtSetPage_KeyPress(object sender, KeyPressEventArgs e) { if ((e.KeyChar >= 48 && e.KeyChar <= 57 ) || e.KeyChar == 8 || e.KeyChar == 13 || e.KeyChar == 127 ) { e.Handled = false ; if (e.KeyChar == 13 ) { Go(); } } else { e.Handled = true ; } } /// <summary> /// 指定頁 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void linkGo_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { if (e.Button == MouseButtons.Left) { Go(); } } private void Go() { if (string.IsNullOrEmpty(txtCurrentPage.Text)) { MessageBox.Show( "指定頁不能為空。" , "提示" , MessageBoxButtons.OK, MessageBoxIcon.Information); txtCurrentPage.Focus(); return ; } if ( int .Parse(txtCurrentPage.Text) > PageCount) { MessageBox.Show( "指定頁已超過總頁數(shù)。" , "提示" , MessageBoxButtons.OK, MessageBoxIcon.Information); txtCurrentPage.Focus(); return ; } PageIndex = int .Parse(txtCurrentPage.Text) - 1 ; SetPage(); } /// <summary> /// linkFirst鼠標(biāo)移過顏色 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void linkFirst_MouseMove(object sender, MouseEventArgs e) { linkFirst.LinkColor = Color.Red; } /// <summary> /// linkFirst鼠標(biāo)離開顏色 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void linkFirst_MouseLeave(object sender, EventArgs e) { linkFirst.LinkColor = Color.Black; } /// <summary> /// linkPrev鼠標(biāo)移過顏色 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void linkPrev_MouseMove(object sender, MouseEventArgs e) { linkPrev.LinkColor = Color.Red; } /// <summary> /// linkPrev鼠標(biāo)離開顏色 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void linkPrev_MouseLeave(object sender, EventArgs e) { linkPrev.LinkColor = Color.Black; } /// <summary> /// linkNext鼠標(biāo)移過顏色 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void linkNext_MouseMove(object sender, MouseEventArgs e) { linkNext.LinkColor = Color.Red; } /// <summary> /// linkNext鼠標(biāo)離開顏色 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void linkNext_MouseLeave(object sender, EventArgs e) { linkNext.LinkColor = Color.Black; } /// <summary> /// linkLast鼠標(biāo)移過顏色 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void linkLast_MouseMove(object sender, MouseEventArgs e) { linkLast.LinkColor = Color.Red; } /// <summary> /// linkLast鼠標(biāo)離開顏色 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void linkLast_MouseLeave(object sender, EventArgs e) { linkLast.LinkColor = Color.Black; } /// <summary> /// linkGo鼠標(biāo)移過顏色 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void linkGo_MouseMove(object sender, MouseEventArgs e) { linkGo.LinkColor = Color.Red; } /// <summary> /// linkGo鼠標(biāo)離開顏色 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void linkGo_MouseLeave(object sender, EventArgs e) { linkGo.LinkColor = Color.Black; } } |
3、SQL Server創(chuàng)建存儲過程PageTest:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
CREATE PROCEDURE [dbo].[PageTest] @PageSize INT, @PageIndex INT, @TotalCount INT OUTPUT AS BEGIN --總記錄數(shù) SELECT @TotalCount =COUNT( 1 ) FROM MF_MO --記錄返回(使用動態(tài)SQL繞開參數(shù)嗅探問題,效率大幅度提升。) DECLARE @SQL NVARCHAR( 1000 ) SET @SQL = 'SELECT TOP (' +CONVERT(VARCHAR( 32 ), @PageSize )+ ') MO_NO,MRP_NO,QTY,BIL_NO ' + 'FROM MF_MO A ' + 'WHERE NOT EXISTS (SELECT 1 FROM (SELECT TOP (' +CONVERT(VARCHAR( 32 ),( @PageIndex - 1 )* @PageSize )+ ') MO_NO FROM MF_MO ORDER BY MO_NO) B WHERE A.MO_NO=B.MO_NO) ' + 'ORDER BY MO_NO' EXEC ( @SQL ) END |
4、新建一個WinForm程序,命名為Main,并拖入一個DataGridView控件及上面新建的PageControl控件,代碼如下:
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
|
private void Main_Load(object sender, EventArgs e) { pageControl1.PageSize = 20 ; pageControl1.PageIndex = 0 ; pageControl1.BindPageEvent += BindPage; pageControl1.SetPage(); } /// <summary> /// 綁定頁 /// </summary> /// <param name="pageSize">每頁顯示記錄數(shù)</param> /// <param name="pageIndex">頁序號</param> /// <param name="totalCount">總記錄數(shù)</param> private void BindPage( int pageSize, int pageIndex, out int totalCount) { SqlConnection conn = null ; SqlCommand cmd = null ; totalCount = 0 ; #region 連接數(shù)據(jù)庫測試 try { //數(shù)據(jù)庫連接 conn = new SqlConnection( "server=.;database=DB_TEST;Uid=sa;pwd=********;" ); conn.Open(); //SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "PageTest" ; cmd.CommandType = CommandType.StoredProcedure; SqlParameter[] param = { new SqlParameter( "@PageSize" ,SqlDbType.Int), new SqlParameter( "@PageIndex" ,SqlDbType.Int), new SqlParameter( "@TotalCount" ,SqlDbType.Int) }; param[ 0 ].Value = pageSize; param[ 1 ].Value = pageIndex; param[ 2 ].Direction = ParameterDirection.Output; cmd.Parameters.AddRange(param); //DataTable DataTable dt = new DataTable( "MF_MO" ); dt.Columns.Add( new DataColumn( "MO_NO" , typeof(String))); dt.Columns.Add( new DataColumn( "MRP_NO" , typeof(String))); dt.Columns.Add( new DataColumn( "QTY" , typeof(Decimal))); dt.Columns.Add( new DataColumn( "BIL_NO" , typeof(String))); #region 方法一:SqlDataReader SqlDataReader dr = cmd.ExecuteReader(); dt.Load(dr, LoadOption.PreserveChanges); dr.Close(); totalCount = ( int )param[ 2 ].Value; dataGridView1.DataSource = dt; #endregion #region #方法二:SqlDataAdapter //SqlDataAdapter da = new SqlDataAdapter(); //da.SelectCommand = cmd; //dt.BeginLoadData(); //da.Fill(dt); //dt.EndLoadData(); //totalCount = (int)param[2].Value; //dataGridView1.DataSource = dt; #endregion } catch (Exception ex) { MessageBox.Show(ex.Message, "提示" , MessageBoxButtons.OK, MessageBoxIcon.Information); } finally { conn.Close(); cmd.Dispose(); } #endregion } |
5、執(zhí)行程序:
總結(jié)
以上所述是小編給大家介紹的DataGridView使用自定義控件實現(xiàn)簡單分頁功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對服務(wù)器之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
原文鏈接:https://www.cnblogs.com/atomy/p/11854991.html