本人不怎么会描述,直接上代码:
Code
1/**//// <summary>
2 /// 获取总页数
3 /// </summary>
4 /// <param name="recordcount">总记录数</param>
5 /// <param name="page_size"></param>
6 /// <returns></returns>
7 public static int GetPageCount(int recordcount, int page_size)
8 {
9 int pagecount = 0;
10 if (recordcount % page_size == 0)
11 pagecount = recordcount / page_size;
12 else
13 pagecount = (recordcount / page_size) + 1;
14
15 return pagecount;
16 }
17
18 /**//// <summary>
19 /// 获取分页数据
20 /// </summary>
21 /// <param name="kl2">DATASET</param>
22 /// <param name="page_size">每页记录数</param>
23 /// <param name="pageindex">页索引</param>
24 /// <param name="page_sum">总页数</param>
25 /// <returns></returns>
26 public static DataView GetPagerForView(DataTable dt, int page_size, int pageindex,out string msg)
27 {
28
29 DataView dv = new DataView();
30
31 if (dt != null)
32 {
33 int recordCount = dt.Rows.Count; //总记录数
34 int page_sum = GetPageCount(recordCount, page_size);
35 if (page_size < dt.Rows.Count)//kl2 :SQL查询函数返回的DATASET
36 {
37 if (page_size == 0)//text_intpase :判断用户设置的分页是否合法
38 page_size = 10;
39 //recordCount = kl2.Tables[0].Rows.Count;//假设每页只显示1条数据,则共可以显示的页数:pagemark页
40 if (page_size < 1)
41 {
42 msg = "请将分页项设置在[1-" + dt.Rows.Count.ToString() + "]之间";
43 }
44
45
46 msg = "共" + page_sum.ToString() + "页," + dt.Rows.Count.ToString() + "条";//page_num :lable
47 DataTable page_table = new DataTable();//记录当前正在操作的是哪个表,全局变量,值由查询函数获取
48
49 for (int k = 0; k < dt.Columns.Count; k++)
50 {
51 page_table.Columns.Add(dt.Columns[k].ColumnName);
52 }
53 if (dt.Rows.Count != 0 && page_size < dt.Rows.Count)
54 {
55 page_table.Clear();
56 try //普通页面显示
57 {
58 page_table.Clear();
59 for (int i = 0; i < page_size; i++)
60 {
61 page_table.Rows.Add(dt.Rows[i + (pageindex * page_size)].ItemArray);
62 }
63 }
64 catch //最后不足一个页面的显示
65 {
66 page_table.Clear();
67 try
68 {
69 for (int s = 0; s < recordCount - (pageindex * page_size); s++)
70 {
71 page_table.Rows.Add(dt.Rows[s + (pageindex * page_size)].ItemArray);
72 }
73 }
74 catch { }
75 }
76 msg += " 当前第" + (pageindex + 1).ToString() + "页";
77 }
78 dv = page_table.DefaultView;
79 }
80 else
81 {
82 dv = dt.DefaultView;
83 msg = "共1页," + dt.Rows.Count.ToString() + "条";
84 msg += " 当前第" + (pageindex + 1).ToString() + "页";
85
86 }
87 return dv;
88 }
89 else
90 {
91 msg = "没有数据!";
92 return null;
93 }
94 }
1/**//// <summary>
2 /// 获取总页数
3 /// </summary>
4 /// <param name="recordcount">总记录数</param>
5 /// <param name="page_size"></param>
6 /// <returns></returns>
7 public static int GetPageCount(int recordcount, int page_size)
8 {
9 int pagecount = 0;
10 if (recordcount % page_size == 0)
11 pagecount = recordcount / page_size;
12 else
13 pagecount = (recordcount / page_size) + 1;
14
15 return pagecount;
16 }
17
18 /**//// <summary>
19 /// 获取分页数据
20 /// </summary>
21 /// <param name="kl2">DATASET</param>
22 /// <param name="page_size">每页记录数</param>
23 /// <param name="pageindex">页索引</param>
24 /// <param name="page_sum">总页数</param>
25 /// <returns></returns>
26 public static DataView GetPagerForView(DataTable dt, int page_size, int pageindex,out string msg)
27 {
28
29 DataView dv = new DataView();
30
31 if (dt != null)
32 {
33 int recordCount = dt.Rows.Count; //总记录数
34 int page_sum = GetPageCount(recordCount, page_size);
35 if (page_size < dt.Rows.Count)//kl2 :SQL查询函数返回的DATASET
36 {
37 if (page_size == 0)//text_intpase :判断用户设置的分页是否合法
38 page_size = 10;
39 //recordCount = kl2.Tables[0].Rows.Count;//假设每页只显示1条数据,则共可以显示的页数:pagemark页
40 if (page_size < 1)
41 {
42 msg = "请将分页项设置在[1-" + dt.Rows.Count.ToString() + "]之间";
43 }
44
45
46 msg = "共" + page_sum.ToString() + "页," + dt.Rows.Count.ToString() + "条";//page_num :lable
47 DataTable page_table = new DataTable();//记录当前正在操作的是哪个表,全局变量,值由查询函数获取
48
49 for (int k = 0; k < dt.Columns.Count; k++)
50 {
51 page_table.Columns.Add(dt.Columns[k].ColumnName);
52 }
53 if (dt.Rows.Count != 0 && page_size < dt.Rows.Count)
54 {
55 page_table.Clear();
56 try //普通页面显示
57 {
58 page_table.Clear();
59 for (int i = 0; i < page_size; i++)
60 {
61 page_table.Rows.Add(dt.Rows[i + (pageindex * page_size)].ItemArray);
62 }
63 }
64 catch //最后不足一个页面的显示
65 {
66 page_table.Clear();
67 try
68 {
69 for (int s = 0; s < recordCount - (pageindex * page_size); s++)
70 {
71 page_table.Rows.Add(dt.Rows[s + (pageindex * page_size)].ItemArray);
72 }
73 }
74 catch { }
75 }
76 msg += " 当前第" + (pageindex + 1).ToString() + "页";
77 }
78 dv = page_table.DefaultView;
79 }
80 else
81 {
82 dv = dt.DefaultView;
83 msg = "共1页," + dt.Rows.Count.ToString() + "条";
84 msg += " 当前第" + (pageindex + 1).ToString() + "页";
85
86 }
87 return dv;
88 }
89 else
90 {
91 msg = "没有数据!";
92 return null;
93 }
94 }
Form中调用:
Code
1private DataTable GetDicData()//获取所有据
2 {
3 BLLB_CommunitDIC bcdicBll = new BLLB_CommunitDIC();
4 if (trvDic.SelectedNode != null)
5 {
6 if (trvDic.SelectedNode.Parent == null)
7 {
8 return null;
9 }
10 else
11 {
12 int areaid = Convert.ToInt32(trvDic.SelectedNode.Tag);
13
14 return FCT.Common.PublicMethods.ConvertToDataSet(bcdicBll.GetAll(areaid)).Tables[0];
15 }
16 }
17 else
18 return null;
19 }
20//根据页索此获取数据
21 private void InitData(int pageindex)
22 {
23 DataTable dt = GetDicData();
24 string msg;
25 if (dt != null && dt.Rows.Count > 0)
26 {
27 dgrvDic.AutoGenerateColumns = false;
28 dgrvDic.DataSource = Public.CustomerPager.GetPagerForView(dt, 10, pageindex, out msg);
29 toolStripLabel18.Text = msg;
30 }
31 }
1private DataTable GetDicData()//获取所有据
2 {
3 BLLB_CommunitDIC bcdicBll = new BLLB_CommunitDIC();
4 if (trvDic.SelectedNode != null)
5 {
6 if (trvDic.SelectedNode.Parent == null)
7 {
8 return null;
9 }
10 else
11 {
12 int areaid = Convert.ToInt32(trvDic.SelectedNode.Tag);
13
14 return FCT.Common.PublicMethods.ConvertToDataSet(bcdicBll.GetAll(areaid)).Tables[0];
15 }
16 }
17 else
18 return null;
19 }
20//根据页索此获取数据
21 private void InitData(int pageindex)
22 {
23 DataTable dt = GetDicData();
24 string msg;
25 if (dt != null && dt.Rows.Count > 0)
26 {
27 dgrvDic.AutoGenerateColumns = false;
28 dgrvDic.DataSource = Public.CustomerPager.GetPagerForView(dt, 10, pageindex, out msg);
29 toolStripLabel18.Text = msg;
30 }
31 }
Code
1分页事件#region 分页事件
2 private void toolStrip7_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
3 {
4 if (e.ClickedItem.Tag != null)
5 {
6 this.Cursor = Cursors.WaitCursor;
7 int _currentPageIndex = 1;
8 DataTable dt = GetDicData();
9 int pagecount = 0;
10 if (dt != null)
11 pagecount = Public.CustomerPager.GetPageCount(dt.Rows.Count, 10);
12
13 if (toolStripTextBox6.Text != "")
14 {
15 try
16 {
17 _currentPageIndex = Convert.ToInt32(toolStripTextBox6.Text);
18 }
19 catch
20 {
21 _currentPageIndex = 1;
22 }
23 }
24
25
26
27 switch (e.ClickedItem.Tag.ToString())
28 {
29 case "First":
30 _currentPageIndex = 1;
31 break;
32 case "Prev":
33 _currentPageIndex = _currentPageIndex - 1;
34 break;
35 case "Next":
36 _currentPageIndex = _currentPageIndex + 1;
37 break;
38 case "Last":
39 _currentPageIndex = pagecount;
40 break;
41 default:
42 this.Cursor = Cursors.Default;
43 break;
44 }
45
46 InitData(_currentPageIndex - 1);
47
48 if (_currentPageIndex == 1)
49 {
50 toolStripTextBox6.Text = "1";
51 this.toolStripButton25.Enabled = false;
52 this.toolStripButton26.Enabled = false;
53 this.toolStripButton27.Enabled = true;
54 this.toolStripButton28.Enabled = true;
55 }
56 else
57 {
58 if (_currentPageIndex < pagecount && _currentPageIndex >= 1)
59 {
60 toolStripTextBox6.Text = _currentPageIndex.ToString();
61 this.toolStripButton25.Enabled = true;
62 this.toolStripButton26.Enabled = true;
63 this.toolStripButton27.Enabled = true;
64 this.toolStripButton28.Enabled = true;
65 }
66 else if (_currentPageIndex == pagecount)
67 {
68 toolStripTextBox6.Text = pagecount.ToString();
69 this.toolStripButton25.Enabled = true;
70 this.toolStripButton26.Enabled = true;
71 this.toolStripButton27.Enabled = false;
72 this.toolStripButton28.Enabled = false;
73 }
74 else
75 {
76 this.toolStripButton25.Enabled = false;
77 this.toolStripButton26.Enabled = false;
78 this.toolStripButton27.Enabled = false;
79 this.toolStripButton28.Enabled = false;
80 }
81 }
82 this.Cursor = Cursors.Default;
83 }
84 #endregion
1分页事件#region 分页事件
2 private void toolStrip7_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
3 {
4 if (e.ClickedItem.Tag != null)
5 {
6 this.Cursor = Cursors.WaitCursor;
7 int _currentPageIndex = 1;
8 DataTable dt = GetDicData();
9 int pagecount = 0;
10 if (dt != null)
11 pagecount = Public.CustomerPager.GetPageCount(dt.Rows.Count, 10);
12
13 if (toolStripTextBox6.Text != "")
14 {
15 try
16 {
17 _currentPageIndex = Convert.ToInt32(toolStripTextBox6.Text);
18 }
19 catch
20 {
21 _currentPageIndex = 1;
22 }
23 }
24
25
26
27 switch (e.ClickedItem.Tag.ToString())
28 {
29 case "First":
30 _currentPageIndex = 1;
31 break;
32 case "Prev":
33 _currentPageIndex = _currentPageIndex - 1;
34 break;
35 case "Next":
36 _currentPageIndex = _currentPageIndex + 1;
37 break;
38 case "Last":
39 _currentPageIndex = pagecount;
40 break;
41 default:
42 this.Cursor = Cursors.Default;
43 break;
44 }
45
46 InitData(_currentPageIndex - 1);
47
48 if (_currentPageIndex == 1)
49 {
50 toolStripTextBox6.Text = "1";
51 this.toolStripButton25.Enabled = false;
52 this.toolStripButton26.Enabled = false;
53 this.toolStripButton27.Enabled = true;
54 this.toolStripButton28.Enabled = true;
55 }
56 else
57 {
58 if (_currentPageIndex < pagecount && _currentPageIndex >= 1)
59 {
60 toolStripTextBox6.Text = _currentPageIndex.ToString();
61 this.toolStripButton25.Enabled = true;
62 this.toolStripButton26.Enabled = true;
63 this.toolStripButton27.Enabled = true;
64 this.toolStripButton28.Enabled = true;
65 }
66 else if (_currentPageIndex == pagecount)
67 {
68 toolStripTextBox6.Text = pagecount.ToString();
69 this.toolStripButton25.Enabled = true;
70 this.toolStripButton26.Enabled = true;
71 this.toolStripButton27.Enabled = false;
72 this.toolStripButton28.Enabled = false;
73 }
74 else
75 {
76 this.toolStripButton25.Enabled = false;
77 this.toolStripButton26.Enabled = false;
78 this.toolStripButton27.Enabled = false;
79 this.toolStripButton28.Enabled = false;
80 }
81 }
82 this.Cursor = Cursors.Default;
83 }
84 #endregion
/** toolStripButton25:首页 toolStripButton26:上一页 toolStripButton27:下一页 toolStripButton28:末页 **/
写的有点乱,希望各位能明白,呵呵~~~