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

DataGridView实现简单分页功能

程序员文章站 2023-10-29 12:22:16
本例子使用自定义控件方法实现,数据库使用的是SQL Server,实现过程如下: 1、新建一个自定义控件,命名为:PageControl。 2、PageControl代码如下: public partial class PageControl : UserControl { //委托及事件 publ ......

    本例子使用自定义控件方法实现,数据库使用的是sql server,实现过程如下:

    1、新建一个自定义控件,命名为:pagecontrol。

    DataGridView实现简单分页功能

    2、pagecontrol代码如下:

    public partial class pagecontrol : usercontrol
    {
        //委托及事件
        public delegate void bindpage(int pagesize, int pageindex, out int totalcount);
        public event bindpage bindpageevent;

        //属性
        public int pagesize { get; set; } = 1;  //每页显示记录数
        public int pageindex { get; set; }      //页序号
        public int totalcount { get; set; }     //总记录数

        public int pagecount { get; set; }      //总页数

        public pagecontrol()
        {
            initializecomponent();
        }

        //设置页
        public void setpage()
        {
            //总记录数
            int totalcount = 0;
            bindpageevent(pagesize, pageindex + 1, out totalcount);
            totalcount = totalcount;

            //总页数
            if (totalcount % pagesize == 0)
                pagecount = totalcount / pagesize;
            else
                pagecount = (int)(totalcount / pagesize) + 1;

            //序号
            lblserialnumber.text = (pageindex + 1).tostring() + "/" + pagecount.tostring();
        }

        /// <summary>
        /// 首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void linkfirst_linkclicked(object sender, linklabellinkclickedeventargs e)
        {
            if (e.button == mousebuttons.left)
            {
                pageindex = 0;
                setpage();
            }
        }

        /// <summary>
        /// 上一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void linkprve_linkclicked(object sender, linklabellinkclickedeventargs e)
        {
            if (e.button == mousebuttons.left)
            {
                pageindex--;
                if (pageindex < 0)
                {
                    pageindex = 0;
                }
                setpage();
            }
        }

        /// <summary>
        /// 下一页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void linknext_linkclicked(object sender, linklabellinkclickedeventargs e)
        {
            if (e.button == mousebuttons.left)
            {
                pageindex++;
                if (pageindex > pagecount - 1)
                {
                    pageindex = pagecount - 1;
                }
                setpage();
            }
        }

        /// <summary>
        /// 末页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void linklast_linkclicked(object sender, linklabellinkclickedeventargs e)
        {
            if (e.button == mousebuttons.left)
            {
                pageindex = pagecount - 1;
                setpage();
            }
        }

        /// <summary>
        /// 只能输入正整数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtsetpage_keypress(object sender, keypresseventargs e)
        {
            if (e.keychar >= 48 && e.keychar <= 57)
                e.handled = false;
            else
                e.handled = true;
        }

        /// <summary>
        /// 指定页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void linkgo_linkclicked(object sender, linklabellinkclickedeventargs e)
        {
            if (e.button == mousebuttons.left)
            {
                if (string.isnullorempty( txtsetpage.text))
                {
                    messagebox.show("指定页不能为空。", "提示", messageboxbuttons.ok, messageboxicon.information);
                    txtsetpage.focus();
                    return;
                }

                if(int.parse(txtsetpage.text)>pagecount)
                {
                    messagebox.show("指定页已超过总页数。", "提示", messageboxbuttons.ok, messageboxicon.information);
                    txtsetpage.focus();
                    return;
                }

                pageindex = int.parse(txtsetpage.text) - 1;
                setpage();
            }
        }
    }

    3、sql server创建存储过程pagetest:

create procedure pagetest
    @pagesize int,
    @pageindex int,
    @totalcount int output
as
begin
    select @totalcount=count(1) from mf_mo

    select top (@pagesize) mo_no,mrp_no,qty,bil_no
    from mf_mo a
    where not exists (select 1 from (select top ((@pageindex-1)*@pagesize) mo_no from mf_mo) b where a.mo_no=b.mo_no)
end

    4、新建一个winform程序,命名为main,并拖入一个datagridview控件及上面新建的pagecontrol控件,代码如下:

        private void main_load(object sender, eventargs e)
        {
            pagecontrol1.pagesize = 20;
            pagecontrol1.pageindex = 0;
            pagecontrol1.bindpageevent += bindpage;
            pagecontrol1.setpage();
        }

        private void bindpage(int pagesize, int pageindex, out int totalcount)
        {
            sqlconnection conn = null;
            sqlcommand cmd = null;
            totalcount = 0;

            #region 连接数据库测试
            try
            {
                //数据库连接
                conn = new sqlconnection("server=.;database=db_test;uid=sa;pwd=********;");
                conn.open();

                //sqlcommand
                cmd = new sqlcommand();
                cmd.connection = conn;
                cmd.commandtext = "pagetest";
                cmd.commandtype = commandtype.storedprocedure;

                sqlparameter[] param =
                {
                    new sqlparameter("@pagesize",sqldbtype.int),
                    new sqlparameter("@pageindex",sqldbtype.int),
                    new sqlparameter("@totalcount",sqldbtype.int)
                };
                param[0].value = pagesize;
                param[1].value = pageindex;
                param[2].direction = parameterdirection.output;
                cmd.parameters.addrange(param);

                //datatable
                datatable dt = new datatable("mf_mo");
                dt.columns.add(new datacolumn("mo_no", typeof(string)));
                dt.columns.add(new datacolumn("mrp_no", typeof(string)));
                dt.columns.add(new datacolumn("qty", typeof(decimal)));
                dt.columns.add(new datacolumn("bil_no", typeof(string)));

                #region 方法一:sqldatareader
                sqldatareader dr = cmd.executereader();
                dt.load(dr, loadoption.preservechanges);
                dr.close();
                totalcount = (int)param[2].value;
                datagridview1.datasource = dt;
                #endregion

                #region #方法二:sqldataadapter
                //sqldataadapter da = new sqldataadapter();
                //da.selectcommand = cmd;
                //dt.beginloaddata();
                //da.fill(dt);
                //dt.endloaddata();
                //totalcount = (int)param[2].value;
                //datagridview1.datasource = dt;
                #endregion
            }
            catch (exception ex)
            {
                messagebox.show(ex.message, "提示", messageboxbuttons.ok, messageboxicon.information);
            }
            finally
            {
                conn.close();
                cmd.dispose();
            }
            #endregion
        }

    5、执行程序:

DataGridView实现简单分页功能

    感觉在200页以内,速度还是可以接受。

    好了,分享就到此结束了,希望对有此需要的人有一些帮助。