DataGridView 的分页处理
2006-09-19 17:15:46 来源:WEB开发网只是解决DataGridview刷新慢的办法。
如果要考虑效率,还是结合分页的"存储过程"比较好,只是变通了一下。
1 #region 数据处理
2 /// <summary>
3 /// 得到期初数据
4 /// </summary>
5 PRivate void getFillDateset()
6 {
7 try
8 {
9 //每页显示数
10 pageSize = 10;
11 //得到最大记录数
12 maxRec = dtSource.Rows.Count;
13 //共有多少页
14 pageCount = (maxRec / pageSize);
15 //取余数
16 if ((maxRec % pageSize) > 0)
17 {
18 pageCount++;
19 }
20 //默认第一页
21 currentPage = 1;
22 recNo = 0;
23 LoadPage();
24 }
25 catch
26 {
27 }
28 }
29
30
31 /// <summary>
32 /// 判断是否数据已经加载
33 /// </summary>
34 /// <returns></returns>
35 private bool CheckFillButton()
36 {
37 if (pageSize == 0) return false;
38 else return true;
39 }
40
41 /// <summary>
42 /// 取DataTable的数据
43 /// </summary>
44 private void LoadPage()
45 {
46 int startRec;
47 int endRec;
48 DataTable dtTemp;
49
50
51 dtTemp = dtSource.Clone();
52 if (currentPage == pageCount) endRec = maxRec;
53 else endRec = pageSize * currentPage;
54 startRec = recNo;
55 for (int i = startRec; i < endRec; i++)
56 {
57 dtTemp.ImportRow(dtSource.Rows[i]);
58 recNo++;
59 }
60 this.dataGridView1.DataSource = dtTemp;
61 }
62
63 /// <summary>
64 /// 分页事件(取各个按键的Tag值)
65 /// </summary>
66 /// <param name="sender"></param>
67 /// <param name="e"></param>
68 private void changepage(object sender, EventArgs e)
69 {
70 if (!CheckFillButton()) return;
71 int myint = Convert.ToInt16((string)(sender as Button).Tag);
72 switch (myint)
73 {
74 case 1:
75 if (currentPage == 1)
76 {
77 MessageBox.Show("The frist page now!");
78 return;
79
80 }
81 currentPage = 1;
82 recNo = 0;
83 LoadPage();
84 break;
85 case 2:
86 currentPage++;
87 if (currentPage > pageCount)
88 {
89 currentPage = pageCount;
90 if (recNo == maxRec)
91 {
92 MessageBox.Show("The last page now!");
93 return;
94 }
95 else
96 recNo = pageSize * (currentPage + 1);
97 }
98 LoadPage();
99 break;
100 case 3:
101 if (currentPage == pageCount)
102 recNo = pageSize * (currentPage - 2);
103 currentPage--;
104 if (currentPage < 1)
105 {
106 MessageBox.Show("The frist page now!");
107 currentPage = 1;
108 return;
109 }
110 else
111 recNo = pageSize * (currentPage - 1);
112
113 LoadPage();
114 break;
115
116 case 4:
117 if (!CheckFillButton()) return;
118 if (recNo == maxRec)
119 {
120 MessageBox.Show("The Last page now!");
121 return;
122 }
123 currentPage = pageCount;
124 recNo = pageSize * (currentPage - 1);
125 LoadPage();
126 break;
127 }
128
129 }
130
131 #endregion
http://www.cnblogs.com/azhai/archive/2006/09/18/507624.html