sort page 排序和分页的小例子

所属分类: 软件编程 / C#教程 阅读数: 132
收藏 0 赞 0 分享
复制代码 代码如下:

/* 系统名:SaleManage
* 模块名:SortPags
* 模块说明:排序分页类(传入DataTable,及相关信息,然后分页,并排序)
* 开发者:Peter Luo
* 开发时间:2012年4月6日
*/
using System;
using System.Collections.Generic;
using System.Linq;
 using System.Text;
 using System.Data ;

 namespace Sale_Core
 {
 public class SortPags
 {
 ///
 /// 存储传入的数据
 ///
 private DataTable _DtSource = null;
 private DataView _DvSource = null;

 ///
 /// 分页排序类
 ///
 /// 要分页或排序的数据源
 public SortPags(DataTable dt)
 {
 this._DtSource = dt;
 }

 ///
 /// 分页排序类
 ///
 /// 要分页或排序的数据源
 public SortPags(DataView dv)
 {
 this._DvSource = dv;
 }

 ///
 /// 页面总数
 ///
 private int _PageCount;

 ///
 /// 每页记录数量
 ///
 private int _PageSiz;

 ///
 /// 记录总数
 ///
 private int _RowCount;

 ///
 /// 排序类型
 /// ASC 升序
 /// DESC 降序
 ///
 private SortType _SortKind;

 ///
 /// 记录当前页面Index
 ///
 private int _CurrentPageIndex;

 ///
 /// 数据源
 ///
 public DataTable DtSource
 {
 get
 {
 return _DtSource;
 }
 }

 ///
 /// 页面数量
 ///
 public int PageCount
 {
 get
 {
 return _PageCount;
 }
 }

 ///
 /// 页面显示数量
 ///
 public int PageSize
 {
 get
 {
 return _PageSiz;
 }
 set
 {
 _PageSiz = value;
 }
 }

 ///
 /// 只读、不能写,获取该数据源那数据总数
 ///
 public int RowCount
 {
 get
 {
 return _RowCount;
 }
 }

 public SortType SortKind
 {
 get
 {
 return _SortKind;
 }
 set
 {
 _SortKind = value;
 }
 }

 ///
 /// 记录当前页面Index
 ///
 public int CurrentPageIndex
 {
 get
 {
 return _CurrentPageIndex;
 }
 }

 public DataView Sort(string sortName, SortType sortKind)
 {
 return new DataView();
 }

 ///
 /// 获取按照给定字段分页后的制定页,(排序->分页)
 ///
 /// 传入排序的字段
 /// 排序的类型:SortType.ASC 升序 SortType.DESC 降序
 /// 页面的大小(页面内要显示的记录的数量)
 /// 当前页面的index
 ///
 public DataTable GetCurrentPageSortByFileName(string sortName, SortType sortKind, int pageSize, int currentPageIndex)
 {
 if (pageSize == 0)
 return DtSource;//如果没有填写pagesize那么返回整个数据源
 if (currentPageIndex <= 0)
 return DtSource; //如果没有传入当前页面index,则返回整个数据源
 if (sortName == "")
 return GetCurrentPage(pageSize, currentPageIndex);//如果排序字段没写,则只有分页,不进行排序

 DataView dv = new DataView(DtSource);
 switch (sortKind)
 {
 case SortType.DESC :
 dv.Sort = sortName + "DESC";
 break;
 case SortType .ASC :
 dv.Sort = sortName + "ASC";
 break;
 default :
 break;
 }

 _PageSiz = pageSize;
 _CurrentPageIndex = currentPageIndex;

 this._RowCount = this.DtSource.Rows.Count;
 this._PageCount = this.RowCount / this.PageSize;
 if (_PageCount * PageSize < RowCount) //如果计算出的页面数*页面上的数据量小于记录数,那么页面大小自动+1
 {
 _PageCount++;
 }

 int currentBeginRowIndex = pageSize * (currentPageIndex - 1); //当前页面的开始行
 int currentEndRowIndex = pageSize * currentPageIndex - 1;//当前页面的结束行
 DataTable dtRes = _DtSource.Clone(); //复制数据源表结构
 for (int i = currentBeginRowIndex; i <= currentEndRowIndex; i++) //复制当前页面的数据到新的datatable中
 {
 if (i >= DtSource.Rows.Count)
 break; //当前页面的记录小于该页面应该显示的记录时,就只取当前页面中的数据
 DataRow dr = dtRes.NewRow();
 for (int j = 0; j < _DtSource.Columns.Count; j++)
 {
 dr[j] = dv[i][j];
 }
 dtRes.Rows.Add(dr);
 }
 return dtRes;
 }

 ///
 ///
 ///
 /// 每页面大小(每个页面上记录的数量)
 /// 当前页面索引
 ///
 public DataTable GetCurrentPage(int pageSize, int currentPageIndex)
 {
 if (pageSize ==0)
 {
 return DtSource;//如果没有填写pagesize那么返回整个数据源
 }
 if (currentPageIndex <= 0)
 {
 return DtSource;//如果没有传入当前页面index,则返回整个数据源
 }
 _PageSiz = pageSize;

 _CurrentPageIndex = currentPageIndex;
 this._RowCount = this.DtSource.Rows.Count;
 this._PageCount = this.RowCount / this.PageSize;
 if (_PageCount * PageSize < RowCount) //如果计算出的页面数*页面上的数据量小于记录数,那么页面大小自动+1
 _PageCount++;
 int CurrentBeginRowIndex = PageSize * (currentPageIndex - 1); //当前页面的开始行
 int CurrentEndRowIndex = PageSize * currentPageIndex - 1; //当前页面的结束行
 DataView dv;
 if (_DvSource == null)
 dv = new DataView(DtSource);
 else
 dv = _DvSource;
 DataTable dtRes = _DtSource.Clone(); //复制数据源表结构
 for (int i = CurrentBeginRowIndex; i <= CurrentEndRowIndex; i++) //复制当前页面的数据到新的datatable中
 {
 if (i >= DtSource.Rows.Count) break; //当前页面的记录小于该页面应该显示的记录时,就只取当前页面中的数据
 DataRow dr = dtRes.NewRow();
 for (int j = 0; j < _DtSource.Columns.Count; j++)
 {
 dr[j] = dv[i][j];
 }
 dtRes.Rows.Add(dr);
 }
 return dtRes;
 }
 public enum SortType
 {
 ASC, //升序排列
 DESC //倒序排列
 }
 }
 } 
更多精彩内容其他人还在看

C# 大小写转换(金额)实例代码

C# 大小写转换(金额)实例代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# WinForm中Panel实现用鼠标操作滚动条的实例方法

由于在WinForm中Panel不能直接响应鼠标的滚动事件,只好采用捕获窗体的滚动事件。
收藏 0 赞 0 分享

C#访问应用程序配置文件的方法

C#访问应用程序配置文件的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#读写文件的方法汇总

C#读写文件的方法汇总,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#计算代码执行时间的方法

在一些测试工作时我们需要获得高精度的代码执行时间以比较其效率。
收藏 0 赞 0 分享

C# 动画窗体(AnimateWindow)的小例子

C# 动画窗体(AnimateWindow)的小例子,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# zxing二维码写入的实例代码

C# zxing二维码写入的实例代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

c#判断输入的是不是数字的小例子

c#判断输入的是不是数字的小例子,需要的朋友可以参考一下
收藏 0 赞 0 分享

c# 深拷贝与浅拷贝的区别分析及实例

浅拷贝(影子克隆):只复制对象的基本类型,对象类型,仍属于原来的引用. 深拷贝(深度克隆):不紧复制对象的基本类,同时也复制原对象中的对象.就是说完全是新对象产生的.
收藏 0 赞 0 分享

C#利用com操作excel释放进程的解决方法

最近利用Microsoft.Office.Interop.Excel.Application读取一个excel后,进程中一直存在excel,在网上找了一阵子,其中有几个解决方案
收藏 0 赞 0 分享
查看更多