欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# WinForm自定义通用分页控件

程序员文章站 2022-04-09 21:29:49
大家好,前几天因工作需要要开发一个基于WinForm的小程序。其中要用到分页,最开始的想法找个第三方的dll用一下,但是后来想了想觉得不如自己写一个玩一下 之前的web开发中有各式各样的列表组件基本都带有分页功能,笔者早先也自己写过B/S端的分页组件(利用jquery纯前端方式)。对于WinForm ......

 

 

  大家好,前几天因工作需要要开发一个基于winform的小程序。其中要用到分页,最开始的想法找个第三方的dll用一下,但是后来想了想觉得不如自己写一个玩一下 之前的web开发中有各式各样的列表组件基本都带有分页功能,笔者早先也自己写过b/s端的分页组件(利用jquery纯前端方式)。对于winform的还是第一次。当完成后发现其实要比b/s端的简单,基本上都是基于各种控件的事件和委托来实现的。后面会介绍到委托和事件在自定义组合用户控件中的使用。

---------------------------------------------------------------------------------------------以下进入正题-------------------------------------------------------------------------------------------------------------------

一、创建一个winform项目,我用的是vs2013 基本上vs其他版本都差不多一样

C# WinForm自定义通用分页控件

建立一个名字叫userpagecontroldemo的项目保存在userpagecontroldemo文件夹下

二、创建用户控件

在你的工程项目文件上点击右键会展示出管理菜单,然后选择添加->用户控件

C# WinForm自定义通用分页控件

三、用户控件开发

C# WinForm自定义通用分页控件

1.在用户控件上拖拽你所要的其他控件,变成组合控件

2.添加app.config文件的节点,这里我们为了让这格控件具有更好的拓展性和通用性,我们把它的每页数据长度作成动态可配置的方式.这样我们在不同的画面应用这格控件的时候可以通过修改配置文件的方式进行页面显示数据长度的修改,而不用再去修改程序

C# WinForm自定义通用分页控件

3.ucpage开发中的技术点:

  3.1委托和事件:

C# WinForm自定义通用分页控件

在开发中我们用到了三次委托如上图中所示的"clickpagebutton","changedpagesize","jumppage"单从字面的意思就不难看出,这三个委托分别代表着"点击分页按钮(如:上一页,下一页等)","改变页面数据展示长度","跳转某一页面"。对应的是三个委托的触发事件 这里这样写的目的是为了以后在页面引用这个控件的时候可以以事件触发的方式来实现控件或者控件上的某个子控件的某个动作的响应。这个在后面我们会讲到。

  3.2控件的初始化和缺省值

这里需要初始化数据的就是页面数据展示长度的下拉框(combobox)控件,如果我们的app.config文件中没有配置数据长度的值,那么我们在程序内部会提供一个缺省值去弥补这个漏洞。代码如下:其中 initcboctrl()方法在ucpage的构造函数中调用即可,cbopagesize控件即为combobox控件,如图1所示。keyandvalueentity类为一个自定义的内部类,是为了绑定combobox控件数据源时所用的。在实践中可以就写到主窗体类的下面,也可以单独写成一个类文件,如果写在当前的用户控件类下方则使用internal的访问修饰符即可。如图2所示。

C# WinForm自定义通用分页控件

图1

C# WinForm自定义通用分页控件

 

 

  private void initcboctrl()
        {
            this.cbopagesize.valuemember = "mvalue";
            this.cbopagesize.displaymember = "mtext";
            this.cbopagesize.text = string.empty;
            if (!string.isnullorempty(_cfgpagesize))
            {
                string cfgpagesize = _cfgpagesize.replace(",", ",");
                if (cfgpagesize.endswith(","))
                {
                    cfgpagesize = cfgpagesize.remove(cfgpagesize.length - 1);
                }
                string[] strpagesize = cfgpagesize.split(new char[] { ',' });
                list<string> listpagesize = new list<string>();
                for (int x = 0; x < strpagesize.length; x++)
                {
                    if (!listpagesize.contains(strpagesize[x]) && !string.isnullorempty(strpagesize[x]))
                    {
                        listpagesize.add(strpagesize[x]);
                    }
                }
                list<keyandvalueentity> kve = new list<keyandvalueentity>();
                for (int i = 0; i < listpagesize.count; i++)
                {
                    kve.add(new keyandvalueentity() { mvalue = i, mtext = listpagesize[i] });
                }
                this.cbopagesize.datasource = kve;
            }
            else
            {
                this.cbopagesize.datasource = new list<keyandvalueentity>()
                {
                    new keyandvalueentity() {mvalue = 0,mtext = "10"},
                    new keyandvalueentity() {mvalue = 1,mtext = "20"},
                    new keyandvalueentity() {mvalue = 2,mtext = "50"},
                    new keyandvalueentity() {mvalue = 3,mtext = "100"}
                };
            }
            this.cbopagesize.selectedtext = cbopagesize.items[0] as string;
        }

另附:keyandvalueentity类代码

    internal class keyandvalueentity
    {
        public int mvalue { get; set; }
        public string mtext { get; set; }
    }

 从代码中我们可以看到我们在读取app.config文件时如果没有获取到动态数据,则程序会加载写死的默认数据

  3.3构造函数与变量的声明:

在本例中变量声明基本上就是当前页:currentpage

当前页面展示数据长度:pagesize

当前数据总数:totalrows

当前页数总数:totalpages

以及一些做展示用的相关控件和子控件:(combobox)cbopagesize、(label)pageinfo、(label)totalrows、(textbox)jumppagectrl等相关属性或对象

这些都是为了在引用该控件的画面调用时方便获取和设置值、参数时使用的。

构造函数中主要是一些数据的初始化和控件的事件触发

C# WinForm自定义通用分页控件

其中f\p\n\l分别代表着首页、上一页、下一页、最后页。他们的点击click事件我们都用一个事件来处理,那么就要为他们的各自的tag做标记,以此来区分按的是哪个按钮

C# WinForm自定义通用分页控件

在最后一行处,我们可以看到我们执行了 this.clickpagebuttonevent(this.currentpage);这行代码,这里其实就是声明委托后把事件释放出来等待着被触发,当然,前提是引用此空间的画面必须要注册这个事件,后面我们会讲到。

四、引用画面的开发

C# WinForm自定义通用分页控件

 

 直接拖拽刚才开发好的ucpage控件即可,如上图所示,这里就不再多说datagridview的使用了

 C# WinForm自定义通用分页控件

这里我们能够看到引用画面的基本结构:

1.分页控件的三个事件:

C# WinForm自定义通用分页控件

这里其实就是前面说的分页控件中委托和事件的用法,如上图引用的实例,那么在哪里去引用/注册这些对象/事件呢?

在引用画面的任何地方都可以,一般的,我们在引用画面的构造函数中去引用/注册这些对象或事件

如图所示:

C# WinForm自定义通用分页控件

这部分就是注册和引用ucpage的一些对象、属性和事件。然后再在事件体中去重写你所需要的逻辑代码,如图所示:

C# WinForm自定义通用分页控件

此处我们以页面跳转事件为例展示。

2.数据展示:

数据的展示是在一个自定义的函数(showdatas)中实现的。请参看代码:

 private void showdatas(int currentpage)
        {
            string sql = @" select t.rn, t.totalrows, t.id, t.username, t.userclassname, t.useraddress
                                        from
                                        (
                                            select count(1) over () as totalrows,
                                            row_number() over (order by c.id) as rn,
                                            c.id, c.useraddress, c.userclassname, c.username
                                            from t_currentuser c
                                        ) t
                                        where t.rn between ((" + currentpage + " - 1) * " + this.ucpagedemo.pagesize + ") + 1 and " + currentpage + " *  " + this.ucpagedemo.pagesize;

            datatable dtresult = databaseaccessoperate.getdatatable(sql);
            int totalpages = 0;
            int totalrows = 0;
            if (null == dtresult || dtresult.rows.count == 0)
            {
                this.ucpagedemo.pageinfo.text = string.format("第{0}/{1}页", "1", "1");
                this.ucpagedemo.totalrows.text = @"0";
                this.ucpagedemo.currentpage = 1;
                this.ucpagedemo.totalpages = 1;
            }
            else
            {
                totalrows = convert.toint32(dtresult.rows[0]["totalrows"].tostring());
                totalpages = totalrows % this.ucpagedemo.pagesize == 0 ? totalrows / this.ucpagedemo.pagesize : (totalrows / this.ucpagedemo.pagesize) + 1;
                this.ucpagedemo.pageinfo.text = string.format("第{0}/{1}页", currentpage, totalpages);
                this.ucpagedemo.totalrows.text = totalrows.tostring();
                this.ucpagedemo.currentpage = currentpage;
                this.ucpagedemo.totalpages = totalpages;
            }
            this.dgvdemo.datasource = dtresult;
        }

在获取数据集后,我们可以为ucpage控件的若干属性赋值,具体代码可以到第五部分 “代码”中去参考

3.控件的初始化:

关于datagridview的初始化本次不过多讨论,可以去网上调查它的基本用法

4.运行效果:

C# WinForm自定义通用分页控件

C# WinForm自定义通用分页控件

 

 C# WinForm自定义通用分页控件-->C# WinForm自定义通用分页控件

 

 C# WinForm自定义通用分页控件

C# WinForm自定义通用分页控件

 

 以上是一些运行demo时的画面截图。

------------------------------------------------------------------------------------------------------------分割线---------------------------------------------------------------------------------------------------------

五、代码:

1.app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup>
        <supportedruntime version="v4.0" sku=".netframework,version=v4.5" />
    </startup>
    <appsettings>
        <!--分页控件每页显示数据长度-->
        <!--长度多个选择时,以","号分隔-->
        <add key="ucpagesize" value="10,20,30,50,100,200"/>
    </appsettings>
    <connectionstrings>
        <add name="databaseconnection" connectionstring="server=your ip address;user id=your db id; password=your db pwd; database=your db name;"/>
    </connectionstrings>
</configuration>

2.ucpage:

  2.1 ucpage.designer.cs:

namespace userpagecontroldemo
{
    partial class ucpage
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private system.componentmodel.icontainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.dispose();
            }
            base.dispose(disposing);
        }

        #region 组件设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void initializecomponent()
        {
            this.btnfrist = new system.windows.forms.button();
            this.lblpage = new system.windows.forms.label();
            this.btnpreviou = new system.windows.forms.button();
            this.btnnext = new system.windows.forms.button();
            this.btnlast = new system.windows.forms.button();
            this.cbopagesize = new system.windows.forms.combobox();
            this.label1 = new system.windows.forms.label();
            this.label2 = new system.windows.forms.label();
            this.label3 = new system.windows.forms.label();
            this.label4 = new system.windows.forms.label();
            this.lbltotalrows = new system.windows.forms.label();
            this.txtjumppage = new system.windows.forms.textbox();
            this.suspendlayout();
            //
            // btnfrist
            //
            this.btnfrist.font = new system.drawing.font("宋体", 9f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((byte)(134)));
            this.btnfrist.location = new system.drawing.point(8, 5);
            this.btnfrist.name = "btnfrist";
            this.btnfrist.size = new system.drawing.size(27, 23);
            this.btnfrist.tabindex = 0;
            this.btnfrist.text = "<<";
            this.btnfrist.usevisualstylebackcolor = true;
            //
            // lblpage
            //
            this.lblpage.autosize = true;
            this.lblpage.font = new system.drawing.font("宋体", 9f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((byte)(134)));
            this.lblpage.location = new system.drawing.point(84, 10);
            this.lblpage.name = "lblpage";
            this.lblpage.size = new system.drawing.size(52, 12);
            this.lblpage.tabindex = 1;
            this.lblpage.text = "第1/1页";
            //
            // btnpreviou
            //
            this.btnpreviou.font = new system.drawing.font("宋体", 9f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((byte)(134)));
            this.btnpreviou.location = new system.drawing.point(41, 5);
            this.btnpreviou.name = "btnpreviou";
            this.btnpreviou.size = new system.drawing.size(27, 23);
            this.btnpreviou.tabindex = 2;
            this.btnpreviou.text = "<";
            this.btnpreviou.usevisualstylebackcolor = true;
            //
            // btnnext
            //
            this.btnnext.font = new system.drawing.font("宋体", 9f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((byte)(134)));
            this.btnnext.location = new system.drawing.point(152, 5);
            this.btnnext.name = "btnnext";
            this.btnnext.size = new system.drawing.size(27, 23);
            this.btnnext.tabindex = 3;
            this.btnnext.text = ">";
            this.btnnext.usevisualstylebackcolor = true;
            //
            // btnlast
            //
            this.btnlast.font = new system.drawing.font("宋体", 9f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((byte)(134)));
            this.btnlast.location = new system.drawing.point(185, 5);
            this.btnlast.name = "btnlast";
            this.btnlast.size = new system.drawing.size(27, 23);
            this.btnlast.tabindex = 4;
            this.btnlast.text = ">>";
            this.btnlast.usevisualstylebackcolor = true;
            //
            // cbopagesize
            //
            this.cbopagesize.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.top | system.windows.forms.anchorstyles.right)));
            this.cbopagesize.formattingenabled = true;
            this.cbopagesize.location = new system.drawing.point(347, 7);
            this.cbopagesize.name = "cbopagesize";
            this.cbopagesize.size = new system.drawing.size(46, 20);
            this.cbopagesize.tabindex = 5;
            //
            // label1
            //
            this.label1.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.top | system.windows.forms.anchorstyles.right)));
            this.label1.autosize = true;
            this.label1.font = new system.drawing.font("宋体", 9f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((byte)(134)));
            this.label1.location = new system.drawing.point(314, 10);
            this.label1.name = "label1";
            this.label1.size = new system.drawing.size(31, 12);
            this.label1.tabindex = 6;
            this.label1.text = "每页";
            //
            // label2
            //
            this.label2.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.top | system.windows.forms.anchorstyles.right)));
            this.label2.autosize = true;
            this.label2.font = new system.drawing.font("宋体", 9f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((byte)(134)));
            this.label2.location = new system.drawing.point(397, 10);
            this.label2.name = "label2";
            this.label2.size = new system.drawing.size(18, 12);
            this.label2.tabindex = 7;
            this.label2.text = "条";
            //
            // label3
            //
            this.label3.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.top | system.windows.forms.anchorstyles.right)));
            this.label3.autosize = true;
            this.label3.font = new system.drawing.font("宋体", 9f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((byte)(134)));
            this.label3.location = new system.drawing.point(432, 10);
            this.label3.name = "label3";
            this.label3.size = new system.drawing.size(18, 12);
            this.label3.tabindex = 8;
            this.label3.text = "共";
            //
            // label4
            //
            this.label4.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.top | system.windows.forms.anchorstyles.right)));
            this.label4.autosize = true;
            this.label4.font = new system.drawing.font("宋体", 9f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((byte)(134)));
            this.label4.location = new system.drawing.point(489, 10);
            this.label4.name = "label4";
            this.label4.size = new system.drawing.size(18, 12);
            this.label4.tabindex = 9;
            this.label4.text = "条";
            //
            // lbltotalrows
            //
            this.lbltotalrows.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.top | system.windows.forms.anchorstyles.right)));
            this.lbltotalrows.autosize = true;
            this.lbltotalrows.font = new system.drawing.font("宋体", 9f, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ((byte)(134)));
            this.lbltotalrows.location = new system.drawing.point(456, 10);
            this.lbltotalrows.name = "lbltotalrows";
            this.lbltotalrows.size = new system.drawing.size(12, 12);
            this.lbltotalrows.tabindex = 10;
            this.lbltotalrows.text = "0";
            //
            // txtjumppage
            //
            this.txtjumppage.location = new system.drawing.point(240, 5);
            this.txtjumppage.multiline = true;
            this.txtjumppage.name = "txtjumppage";
            this.txtjumppage.size = new system.drawing.size(30, 21);
            this.txtjumppage.tabindex = 11;
            //
            // ucpage
            //
            this.autoscaledimensions = new system.drawing.sizef(6f, 12f);
            this.autoscalemode = system.windows.forms.autoscalemode.font;
            this.controls.add(this.txtjumppage);
            this.controls.add(this.lbltotalrows);
            this.controls.add(this.label4);
            this.controls.add(this.label3);
            this.controls.add(this.label2);
            this.controls.add(this.label1);
            this.controls.add(this.cbopagesize);
            this.controls.add(this.btnlast);
            this.controls.add(this.btnnext);
            this.controls.add(this.btnpreviou);
            this.controls.add(this.lblpage);
            this.controls.add(this.btnfrist);
            this.name = "ucpage";
            this.size = new system.drawing.size(520, 31);
            this.resumelayout(false);
            this.performlayout();

        }

        #endregion

        private system.windows.forms.button btnfrist;
        private system.windows.forms.label lblpage;
        private system.windows.forms.button btnpreviou;
        private system.windows.forms.button btnnext;
        private system.windows.forms.button btnlast;
        private system.windows.forms.combobox cbopagesize;
        private system.windows.forms.label label1;
        private system.windows.forms.label label2;
        private system.windows.forms.label label3;
        private system.windows.forms.label label4;
        private system.windows.forms.label lbltotalrows;
        private system.windows.forms.textbox txtjumppage;
    }
}
2.2ucpage.cs:

namespace userpagecontroldemo
{
    public partial class ucpage : usercontrol
    {
        private string _cfgpagesize = configurationmanager.appsettings["ucpagesize"];

        public delegate void clickpagebutton(int current);
        public event clickpagebutton clickpagebuttonevent;

        public delegate void changedpagesize();
        public event changedpagesize changedpagesizeevent;

        public delegate void jumppage(int jumppage);
        public event jumppage jumppageevent;

        public int totalpages { get; set; }

        private int currentpage;
        public int currentpage
        {
            get { return this.currentpage; }
            set { this.currentpage = value; }
        }

        private int pagesize;
        public int pagesize
        {
            get { return this.pagesize; }
            set { this.pagesize = value; }
        }

        public combobox cbopagesize
        {
            set { this.cbopagesize = value; }
            get { return this.cbopagesize; }
        }

        public label pageinfo
        {
            set { this.lblpage = value; }
            get { return this.lblpage; }
        }

        public label totalrows
        {
            get { return this.lbltotalrows; }
            set { this.lbltotalrows = value; }
        }

        public textbox jumppagectrl
        {
            get { return this.txtjumppage; }
            set { this.txtjumppage = value; }
        }

        public ucpage()
        {
            initializecomponent();
            this.initcboctrl();
            this.cbopagesize.textchanged += cbopagesize_textchanged;
            this.cbopagesize.keypress += cbopagesize_keypress;
            this.btnfrist.tag = "f";
            this.btnpreviou.tag = "p";
            this.btnnext.tag = "n";
            this.btnlast.tag = "l";
            this.btnfrist.click += btn_click;
            this.btnpreviou.click += btn_click;
            this.btnnext.click += btn_click;
            this.btnlast.click += btn_click;
            this.cbopagesize.keypress += cbopagesize_keypress;
            this.txtjumppage.keypress += txtjumppage_keypress;
        }

        void txtjumppage_keypress(object sender, keypresseventargs e)
        {
            //text输入验证
            if (e.keychar == 13)
            {
                if (null != this.jumppageevent)
                {
                    this.jumppageevent(convert.toint32(this.txtjumppage.text));
                }
            }
            else
            {
                if (e.keychar != 8)
                {
                    int len = this.txtjumppage.text.length;
                    if (len < 1 && e.keychar == '0')
                    {
                        e.handled = true;
                    }
                    else if ((e.keychar < '0') || (e.keychar > '9'))//这是允许输入0-9数字
                    {
                        e.handled = true;
                    }
                }
            }
        }
        void btn_click(object sender, eventargs e)
        {
            button btn = sender as button;
            if (null != this.clickpagebuttonevent)
            {
                if (null != btn)
                {
                    switch (btn.tag.tostring())
                    {
                        case "f":
                            this.currentpage = 1;
                            break;
                        case "p":
                            this.currentpage = this.currentpage <= 1 ? 1 : this.currentpage - 1;
                            break;
                        case "n":
                            this.currentpage = this.currentpage + 1;
                            break;
                        case "l":
                            this.currentpage = this.totalpages;
                            break;
                        default:
                            this.currentpage = 1;
                            break;
                    }
                    this.clickpagebuttonevent(this.currentpage);
                }
            }
        }
        void cbopagesize_keypress(object sender, keypresseventargs e)
        {
            e.handled = true;
        }
        void cbopagesize_textchanged(object sender, eventargs e)
        {
            this.pagesize = convert.toint32(this.cbopagesize.text);
            if (null != changedpagesizeevent)
            {
                this.changedpagesizeevent();
            }
        }
        private void initcboctrl()
        {
            this.cbopagesize.valuemember = "mvalue";
            this.cbopagesize.displaymember = "mtext";
            this.cbopagesize.text = string.empty;
            if (!string.isnullorempty(_cfgpagesize))
            {
                string cfgpagesize = _cfgpagesize.replace(",", ",");
                if (cfgpagesize.endswith(","))
                {
                    cfgpagesize = cfgpagesize.remove(cfgpagesize.length - 1);
                }
                string[] strpagesize = cfgpagesize.split(new char[] { ',' });
                list<string> listpagesize = new list<string>();
                for (int x = 0; x < strpagesize.length; x++)
                {
                    if (!listpagesize.contains(strpagesize[x]) && !string.isnullorempty(strpagesize[x]))
                    {
                        listpagesize.add(strpagesize[x]);
                    }
                }
                list<keyandvalueentity> kve = new list<keyandvalueentity>();
                for (int i = 0; i < listpagesize.count; i++)
                {
                    kve.add(new keyandvalueentity() { mvalue = i, mtext = listpagesize[i] });
                }
                this.cbopagesize.datasource = kve;
            }
            else
            {
                this.cbopagesize.datasource = new list<keyandvalueentity>()
                {
                    new keyandvalueentity() {mvalue = 0,mtext = "10"},
                    new keyandvalueentity() {mvalue = 1,mtext = "20"},
                    new keyandvalueentity() {mvalue = 2,mtext = "50"},
                    new keyandvalueentity() {mvalue = 3,mtext = "100"}
                };
            }
            this.cbopagesize.selectedtext = cbopagesize.items[0] as string;
        }
    }

    internal class keyandvalueentity
    {
        public int mvalue { get; set; }
        public string mtext { get; set; }
    }
}

2.3 引用画面:

    public partial class frmpagedemo : form
    {
        public frmpagedemo()
        {
            initializecomponent();
            this.ucpagedemo.currentpage = 1;
            this.ucpagedemo.pagesize = convert.toint32(this.ucpagedemo.cbopagesize.text);
            this.ucpagedemo.totalpages = 1;
            this.ucpagedemo.clickpagebuttonevent += ucpagedemo_clickpagebuttonevent;
            this.ucpagedemo.changedpagesizeevent += ucpagedemo_changedpagesizeevent;
            this.ucpagedemo.jumppageevent += ucpagedemo_jumppageevent;
            this.startposition = formstartposition.centerscreen;
            this.initdatagridviewctrl();
        }
        /// <summary>
        /// 页数跳转
        /// </summary>
        /// <param name="jumppage">跳转页</param>
        void ucpagedemo_jumppageevent(int jumppage)
        {
            if (jumppage <= this.ucpagedemo.totalpages)
            {
                if (jumppage > 0)
                {
                    this.ucpagedemo.jumppagectrl.text = string.empty;
                    this.ucpagedemo.jumppagectrl.text = jumppage.tostring();
                    this.showdatas(jumppage);
                }
                else
                {
                    jumppage = 1;
                    this.ucpagedemo.jumppagectrl.text = string.empty;
                    this.ucpagedemo.jumppagectrl.text = jumppage.tostring();
                    this.showdatas(jumppage);
                }
            }
            else
            {
                this.ucpagedemo.jumppagectrl.text = string.empty;
                messagebox.show(@"超出当前最大页数", @"提示", messageboxbuttons.ok, messageboxicon.information);
            }
        }
        /// <summary>
        /// 改变每页展示数据长度
        /// </summary>
        void ucpagedemo_changedpagesizeevent()
        {
            this.showdatas(1);
        }
        /// <summary>
        /// 页数改变按钮(最前页,最后页,上一页,下一页)
        /// </summary>
        /// <param name="current"></param>
        void ucpagedemo_clickpagebuttonevent(int current)
        {
            this.showdatas(current);
        }
        /// <summary>
        /// 初始化datagridview控件
        /// </summary>
        private void initdatagridviewctrl()
        {
            this.showdatas(1);
        }
        /// <summary>
        /// 数据展示
        /// </summary>
        /// <param name="currentpage">当前页</param>
        private void showdatas(int currentpage)
        {
            string sql = @" select t.rn, t.totalrows, t.id, t.username, t.userclassname, t.useraddress
                                        from
                                        (
                                            select count(1) over () as totalrows,
                                            row_number() over (order by c.id) as rn,
                                            c.id, c.useraddress, c.userclassname, c.username
                                            from t_currentuser c
                                        ) t
                                        where t.rn between ((" + currentpage + " - 1) * " + this.ucpagedemo.pagesize + ") + 1 and " + currentpage + " *  " + this.ucpagedemo.pagesize;

            datatable dtresult = databaseaccessoperate.getdatatable(sql);
            int totalpages = 0;
            int totalrows = 0;
            if (null == dtresult || dtresult.rows.count == 0)
            {
                this.ucpagedemo.pageinfo.text = string.format("第{0}/{1}页", "1", "1");
                this.ucpagedemo.totalrows.text = @"0";
                this.ucpagedemo.currentpage = 1;
                this.ucpagedemo.totalpages = 1;
            }
            else
            {
                totalrows = convert.toint32(dtresult.rows[0]["totalrows"].tostring());
                totalpages = totalrows % this.ucpagedemo.pagesize == 0 ? totalrows / this.ucpagedemo.pagesize : (totalrows / this.ucpagedemo.pagesize) + 1;
                this.ucpagedemo.pageinfo.text = string.format("第{0}/{1}页", currentpage, totalpages);
                this.ucpagedemo.totalrows.text = totalrows.tostring();
                this.ucpagedemo.currentpage = currentpage;
                this.ucpagedemo.totalpages = totalpages;
            }
            this.dgvdemo.datasource = dtresult;
        }
    }

 

以上