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

WinFrom 在Devexpress里用GridControl和DataNavigtor进行分页

程序员文章站 2022-05-28 14:41:39
1,分页嘛先要有个SQL 程序才能写下去 先提供下SQL的思路,对于分页的SQL我之前帖子有介绍,就不一一介绍了 select top pageSize * --显示数量 from (select row_number() over(order by EG_ID asc) as rownumber, ......

1,分页嘛先要有个sql 程序才能写下去 先提供下sql的思路,对于分页的sql我之前帖子有介绍,就不一一介绍了

select top pagesize *     --显示数量
from (select row_number()   
over(order by eg_id asc) as rownumber,* --行数
from exchanggifts) temp_row
where rownumber>((pageindex-1)*pagesize);--开始页数  --建议sql

存储过程参考下

create proc [dbo].[p_getcardlevelspaged]
    @pagesize int,
    @pageindex int,
    @count int output,
    @levelname varchar(20)
as
begin
    select top(@pagesize) * from cardlevels 
    where cl_id not in(
                        select top(@pagesize*(@pageindex-1)) cl_id from cardlevels where cl_levelname like '%'+@levelname+'%'
                      ) 
    and cl_levelname like '%'+@levelname+'%'
    select @count=count(*) from cardlevels where cl_levelname like '%'+@levelname+'%'
end

go

2,例子中用的分页sql:

select * from(select row_number() over(order by e.eg_id) as rows ,e. * from exchanggifts e)
 as a where a.rows between 1 and 10;   

 

 

代码  注释详细

 public partial class xtraform2 : devexpress.xtraeditors.xtraform
    {

        private int pageindex = 1; //当前页码
        private int pagesize = 5;   //每页的尺寸
        private int pagecount = 0; //总页数
      
       public xtraform2()
        {
            initializecomponent();
        }

  //获取记录总数
        public int getrecordcount()
        {
            int count = 0;
            string sql = "select count(*) from exchanggifts";
            count = convert.toint32(dbhelper.getscalar(sql, null, false));
            return count;
        }

        //获取当前页的记录列表
        public datatable getlistbypage(int startindex, int endindex)
        {
            datatable dt = new datatable();
            stringbuilder strsql = new stringbuilder();
            strsql.append("select * from (");
            strsql.append("select row_number() over(order by t.[eg_id]) as rows,t.* from exchanggifts as t) as tt");
            strsql.appendformat(" where tt.rows between {0} and {1} ", startindex, endindex);
            dt = dbhelper.query(strsql.tostring(), null);
            return dt;
        }

        //绑定数据表格控件
        public void bindpagegridlist()
        {
            //记录的开始索引
            int startindex = (pageindex - 1) * pagesize + 1;
            //记录的结束索引
            int endindex = pageindex * pagesize;
            //总记录数
            int row = getrecordcount();

            if (row % pagesize > 0)
            {
                pagecount = row / pagesize + 1;
            }
            else
            {
                pagecount = row / pagesize;
            }
            //如果当前是最后一页,设置最后一行记录的索引
            if (pageindex == pagecount)
            {
                endindex = row;
            }

            datatable dt = this.getlistbypage(startindex, endindex);
            gc.datasource = dt;
            nvgdatapager.datasource = dt;
            nvgdatapager.textstringformat = string.format("第{0}页,共{1}页", pageindex, pagecount);
        }

        private void nvgdatapager_buttonclick(object sender, navigatorbuttonclickeventargs e)
        {
            string type = e.button.tag.tostring();
            switch (type)
            {
                case "首页":
                    pageindex = 1;
                    break;
                case "末页":
                    pageindex = pagecount;
                    break;
                case "下一页":
                    if (pageindex < pagecount)
                    {
                        pageindex++;
                    }
                    break;
                case "上一页":
                    if (pageindex > 1)
                    {
                        pageindex--;
                    }
                    break;
            }
            this.bindpagegridlist(); //绑定当前页到控件
        }

}

效果图

WinFrom 在Devexpress里用GridControl和DataNavigtor进行分页