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(); //绑定当前页到控件 } }
效果图