服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - winform分页控件 附源码下载

winform分页控件 附源码下载

2021-12-06 15:06小锋神 C#

这篇文章主要介绍了winform分页控件,文中附源码下载地址,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

以前都是从事b/s开发,由于公司有个比较大的c/s项目,在使用datagridview的时候,显示数据量比较大,所以才用分页模式,也不知道这样是否正确。 

想找个c/s下面的分页控件,都没有什么好的,就自己跟b/s下的分页控件,修改成winform下面的。 

首先创建一个用户控件名称为pager,在控件中拖入bindingnavigator和bindingsource,修改bindingnavigator,加入必要的一些控件。 

效果如下:

winform分页控件 附源码下载

代码实现如下:

?
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();
    }

效果如下

winform分页控件 附源码下载

源码下载:winformpager.rar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

延伸 · 阅读

精彩推荐
  • C#SQLite在C#中的安装与操作技巧

    SQLite在C#中的安装与操作技巧

    SQLite,是一款轻型的数据库,用于本地的数据储存。其优点有很多,下面通过本文给大家介绍SQLite在C#中的安装与操作技巧,感兴趣的的朋友参考下吧...

    蓝曈魅11162022-01-20
  • C#C#设计模式之Strategy策略模式解决007大破密码危机问题示例

    C#设计模式之Strategy策略模式解决007大破密码危机问题示例

    这篇文章主要介绍了C#设计模式之Strategy策略模式解决007大破密码危机问题,简单描述了策略模式的定义并结合加密解密算法实例分析了C#策略模式的具体使用...

    GhostRider10972022-01-21
  • C#利用C#实现网络爬虫

    利用C#实现网络爬虫

    这篇文章主要介绍了利用C#实现网络爬虫,完整的介绍了C#实现网络爬虫详细过程,感兴趣的小伙伴们可以参考一下...

    C#教程网11852021-11-16
  • C#如何使用C#将Tensorflow训练的.pb文件用在生产环境详解

    如何使用C#将Tensorflow训练的.pb文件用在生产环境详解

    这篇文章主要给大家介绍了关于如何使用C#将Tensorflow训练的.pb文件用在生产环境的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴...

    bbird201811792022-03-05
  • C#C#微信公众号与订阅号接口开发示例代码

    C#微信公众号与订阅号接口开发示例代码

    这篇文章主要介绍了C#微信公众号与订阅号接口开发示例代码,结合实例形式简单分析了C#针对微信接口的调用与处理技巧,需要的朋友可以参考下...

    smartsmile20127762021-11-25
  • C#深入理解C#的数组

    深入理解C#的数组

    本篇文章主要介绍了C#的数组,数组是一种数据结构,详细的介绍了数组的声明和访问等,有兴趣的可以了解一下。...

    佳园9492021-12-10
  • C#三十分钟快速掌握C# 6.0知识点

    三十分钟快速掌握C# 6.0知识点

    这篇文章主要介绍了C# 6.0的相关知识点,文中介绍的非常详细,通过这篇文字可以让大家在三十分钟内快速的掌握C# 6.0,需要的朋友可以参考借鉴,下面来...

    雨夜潇湘8272021-12-28
  • C#VS2012 程序打包部署图文详解

    VS2012 程序打包部署图文详解

    VS2012虽然没有集成打包工具,但它为我们提供了下载的端口,需要我们手动安装一个插件InstallShield。网上有很多第三方的打包工具,但为什么偏要使用微软...

    张信秀7712021-12-15