实现思路:通过分页储存过程返回的数据条数,传递到分页控件里计算,不多说了,看看下面的代码
1.先新建一个控件库,如:
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Drawing;
5 using System.Data;
6 using System.Text;
7 using System.Windows.Forms;
8
9 namespace WinFormPage
10 {
11 public partial class WinFormPage : UserControl
12 {
13 public delegate void RefreshPage();
14 private RefreshPage _refresh;
15 //页显示数
16 private int _PageSize = 20;
17 //页总数
18 private int _PageCount = 0;
19 //页码
20 private int _PageIndex = 1;
21 //数据条数
22 private int _Count = 0;
23 //跳转页码
24 private int _GoIndex = 0;
25
26 /// <summary>
27 /// 获取或设置页显示数量
28 /// </summary>
29 public int PageSize
30 {
31 get { return _PageSize; }
32 set { _PageSize = value; }
33 }
34 /// <summary>
35 /// 获取或设置页数量
36 /// </summary>
37 public int PageCount
38 {
39 get { return _PageCount; }
40 set
41 {
42 _PageCount = value;
43 labpcount.Text = _PageCount.ToString();
44 }
45 }
46
47 /// <summary>
48 /// 获取或设置页码
49 /// </summary>
50 public int PageIndex
51 {
52 get { return Convert.ToInt32(labindex.Text); }
53 set { _PageIndex = value; }
54 }
55
56 /// <summary>
57 /// 获取或设置数据总数量
58 /// </summary>
59 public int Count
60 {
61 get { return _Count; }
62 set { _Count = value; }
63 }
64 /// <summary>
65 /// 获取或设置跳转页面
66 /// </summary>
67 public int GoIndex
68 {
69 get { return _GoIndex; }
70 set { _GoIndex = value; }
71 }
72
73 /// <summary>
74 /// 刷新数据
75 /// </summary>
76 public RefreshPage RefreshData
77 {
78 set { _refresh = value; }
79 }
80
81 /// <summary>
82 /// 构造函数
83 /// </summary>
84 public WinFormPage()
85 {
86 InitializeComponent();
87 }
88
89
90
91
92 private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
93 {
94 //第一页
95 _PageIndex = 1;
96 labindex.Text = _PageIndex.ToString();
97 _refresh();
98 }
99
100 private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
101 {
102 //上一页
103 int tmp = Convert.ToInt32(labindex.Text);
104 tmp = tmp - 1;
105 if (tmp <= 0)
106 {
107 tmp = 1;
108 }
109 _PageIndex = tmp;
110 labindex.Text = _PageIndex.ToString();
111 _refresh();
112 }
113
114 private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
115 {
116 //下一页
117 int tmp = Convert.ToInt32(labpcount.Text);
118 int tmp2 = Convert.ToInt32(labindex.Text);
119 tmp2 = tmp2 + 1;
120 if (tmp2 > tmp)
121 {
122 _PageIndex = tmp;
123 labindex.Text = tmp.ToString();
124 }
125 else
126 {
127 _PageIndex = tmp2;
128 labindex.Text = tmp2.ToString();
129 }
130 _refresh();
131 }
132
133 private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
134 {
135 //最后页
136 _PageIndex = Convert.ToInt32(labpcount.Text);
137 labindex.Text = labpcount.Text;
138 _refresh();
139 }
140
141 private void linkLabel5_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
142 {
143 //跳转
144 int tmp = 0;
145 try
146 {
147 tmp = Convert.ToInt32(tbxGo.Text);
148 }
149 catch
150 {
151 tmp = 1;
152 tbxGo.Text = "1";
153 }
154 if (tmp <= 0)
155 {
156 tmp = 1;
157 }
158 int tmp2 = Convert.ToInt32(labpcount.Text);
159 if (tmp > tmp2)
160 {
161 _PageIndex = tmp2;
162 }
163 else
164 {
165 _PageIndex = tmp;
166 }
167 labindex.Text = _PageIndex.ToString();
168 tbxGo.Text = _PageIndex.ToString();
169 _refresh();
170 }
171 }
172 }
2.生成控件文件后,新建已成应用程序调用,调用代码如下:
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Data.SqlClient;
6 using System.Drawing;
7 using System.Text;
8 using System.Windows.Forms;
9
10 namespace text
11 {
12 public partial class Form1 : Form
13 {
14 public Form1()
15 {
16 InitializeComponent();
17 }
18
19 private void Form1_Load(object sender, EventArgs e)
20 {
21 winFormPage1.RefreshData = GvDataBind;
22 GvDataBind();
23 }
24
25 private void GvDataBind()
26 {
27 SqlConnection con = new SqlConnection("server=.;database=northwind;User Id=sa;pwd=123456");
28 SqlCommand cmd = new SqlCommand();
29 cmd.Connection = con;
30 cmd.CommandText = "proc_ListPage";
31 cmd.CommandType = CommandType.StoredProcedure;
32
33 cmd.Parameters.Add(new SqlParameter("@tblName", SqlDbType.VarChar, 50));
34 cmd.Parameters["@tblName"].Value = "Orders";
35 cmd.Parameters.Add(new SqlParameter("@fldName", SqlDbType.VarChar, 50));
36 cmd.Parameters["@fldName"].Value = "OrderID,ShipName, OrderDate, ShipCity";
37 cmd.Parameters.Add(new SqlParameter("@page", SqlDbType.VarChar, 50));
38 cmd.Parameters["@page"].Value = winFormPage1.PageIndex;
39 cmd.Parameters.Add(new SqlParameter("@pageSize", SqlDbType.VarChar, 50));
40 cmd.Parameters["@pageSize"].Value = winFormPage1.PageSize;
41 cmd.Parameters.Add(new SqlParameter("@fldSort", SqlDbType.VarChar, 50));
42 cmd.Parameters["@fldSort"].Value = "OrderID";
43 cmd.Parameters.Add(new SqlParameter("@Sort", SqlDbType.VarChar, 50));
44 cmd.Parameters["@Sort"].Value = "1";
45 cmd.Parameters.Add(new SqlParameter("@strCondition", SqlDbType.VarChar, 50));
46 cmd.Parameters["@strCondition"].Value = " and (OrderID > 1)";
47 cmd.Parameters.Add(new SqlParameter("@ID", SqlDbType.VarChar, 50));
48 cmd.Parameters["@ID"].Value = "OrderID";
49 cmd.Parameters.Add(new SqlParameter("@Dist", SqlDbType.VarChar, 50));
50 cmd.Parameters["@Dist"].Value = "0";
51
52 cmd.Parameters.Add(new SqlParameter("@pageCount", SqlDbType.Int, 4));
53 cmd.Parameters["@pageCount"].Direction = ParameterDirection.Output;
54 cmd.Parameters.Add(new SqlParameter("@Counts", SqlDbType.Int, 4));
55 cmd.Parameters["@Counts"].Direction = ParameterDirection.Output;
56
57 SqlDataAdapter da = new SqlDataAdapter();
58 da.SelectCommand = cmd;
59 DataSet ds = new DataSet();
60 try
61 {
62 con.Open();
63 da.Fill(ds);
64
65 int PageCount = Convert.ToInt32(cmd.Parameters["@pageCount"].Value);
66 int count = Convert.ToInt32(cmd.Parameters["@Counts"].Value);
67
68 winFormPage1.PageCount = PageCount;
69 winFormPage1.Count = count;
70
71 Gv.DataSource = ds.Tables[0];
72 }
73 catch
74 {
75 }
76 finally
77 {
78 con.Close();
79 }
80
81 }
82 }
83 }
3.配合使用的储存过程,如下:
1
2 CREATE PROCEDURE dbo.proc_ListPage
3 (
4 @tblName nvarchar(200), ----要显示的表或多个表的连接
5 @fldName nvarchar(500) = '*', ----要显示的字段列表
6 @pageSize int = 10, ----每页显示的记录个数
7 @page int = 1, ----要显示那一页的记录
8 @pageCount int = 1 output, ----查询结果分页后的总页数
9 @Counts int = 1 output, ----查询到的记录数
10 @fldSort nvarchar(200) = null, ----排序字段列表或条件
11 @Sort bit = 0, ----排序方法,0为升序,1为降序(如果是多字段排列Sort指代最后一个排序字段的排列顺序(最后一个排序字段不加排序标记)--程序传参如:' SortA Asc,SortB Desc,SortC ')
12 @strCondition nvarchar(1000) = null, ----查询条件,不需where
13 @ID nvarchar(150), ----主表的主键
14 @Dist bit = 0 ----是否添加查询字段的 DISTINCT 默认0不添加/1添加
15 )
16 AS
17 SET NOCOUNT ON
18 Declare @sqlTmp nvarchar(1000) ----存放动态生成的SQL语句
19 Declare @strTmp nvarchar(1000) ----存放取得查询结果总数的查询语句
20 Declare @strID nvarchar(1000) ----存放取得查询开头或结尾ID的查询语句
21
22 Declare @strSortType nvarchar(10) ----数据排序规则A
23 Declare @strFSortType nvarchar(10) ----数据排序规则B
24
25 Declare @SqlSelect nvarchar(50) ----对含有DISTINCT的查询进行SQL构造
26 Declare @SqlCounts nvarchar(50) ----对含有DISTINCT的总数查询进行SQL构造
27
28
29 if @Dist = 0
30 begin
31 set @SqlSelect = 'select '
32 set @SqlCounts = 'Count(*)'
33 end
34 else
35 begin
36 set @SqlSelect = 'select distinct '
37 set @SqlCounts = 'Count(DISTINCT '+@ID+')'
38 end
39
40
41 if @Sort=0
42 begin
43 set @strFSortType=' ASC '
44 set @strSortType=' DESC '
45 end
46 else
47 begin
48 set @strFSortType=' DESC '
49 set @strSortType=' ASC '
50 end
51
52
53
54 --------生成查询语句--------
55 --此处@strTmp为取得查询结果数量的语句
56 if @strCondition is null or @strCondition='' --没有设置显示条件
57 begin
58 set @sqlTmp = @fldName + ' From ' + @tblName
59 set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName
60 set @strID = ' From ' + @tblName
61 end
62 else
63 begin
64 set @sqlTmp = + @fldName + 'From ' + @tblName + ' where (1>0) ' + @strCondition
65 set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName + ' where (1>0) ' + @strCondition
66 set @strID = ' From ' + @tblName + ' where (1>0) ' + @strCondition
67 end
68
69 ----取得查询结果总数量-----
70 exec sp_executesql @strTmp,N'@Counts int out ',@Counts out
71 declare @tmpCounts int
72 if @Counts = 0
73 set @tmpCounts = 1
74 else
75 set @tmpCounts = @Counts
76
77 --取得分页总数
78 set @pageCount=(@tmpCounts+@pageSize-1)/@pageSize
79
80 /**//**当前页大于总页数 取最后一页**/
81 if @page>@pageCount
82 set @page=@pageCount
83
84 --/*-----数据分页2分处理-------*/
85 declare @pageIndex int --总数/页大小
86 declare @lastcount int --总数%页大小
87
88 set @pageIndex = @tmpCounts/@pageSize
89 set @lastcount = @tmpCounts%@pageSize
90 if @lastcount > 0
91 set @pageIndex = @pageIndex + 1
92 else
93 set @lastcount = @pagesize
94
95 --//***显示分页
96 if @strCondition is null or @strCondition='' --没有设置显示条件
97 begin
98 if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2 --前半部分数据处理
99 begin
100 if @page=1
101 set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
102 +' order by '+ @fldSort +' '+ @strFSortType
103 else
104 begin
105 set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
106 +' where '+@ID+' <(select min('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName
107 +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)'
108 +' order by '+ @fldSort +' '+ @strFSortType
109 end
110 end
111 else
112 begin
113 set @page = @pageIndex-@page+1 --后半部分数据处理
114 if @page <= 1 --最后一页数据显示
115 set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName
116 +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
117 else
118 set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
119 +' where '+@ID+' >(select max('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName
120 +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)'
121 +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
122 end
123 end
124
125 else --有查询条件
126 begin
127 if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2 --前半部分数据处理
128 begin
129 if @page=1
130 set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
131 +' where 1=1 ' + @strCondition + ' order by '+ @fldSort +' '+ @strFSortType
132 else
133 begin
134 set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
135 +' where '+@ID+' <(select min('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName
136 +' where (1=1) ' + @strCondition +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)'
137 +' '+ @strCondition +' order by '+ @fldSort +' '+ @strFSortType
138 end
139 end
140 else
141 begin
142 set @page = @pageIndex-@page+1 --后半部分数据处理
143 if @page <= 1 --最后一页数据显示
144 set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName
145 +' where (1=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
146 else
147 set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
148 +' where '+@ID+' >(select max('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName
149 +' where (1=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)'
150 +' '+ @strCondition+' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
151 end
152 end
153
154 ------返回查询结果-----
155 exec sp_executesql @strTmp
156 --print @strTmp
157 SET NOCOUNT OFF
158
159 GO
160
4.最后看看运行的效果:
5.有兴趣的朋友,大家可以交流下怎么去改进下,源码下载地址: