源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

sort page 排序和分页的小例子

  • 时间:2020-06-23 21:17 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:sort page 排序和分页的小例子
[u]复制代码[/u] 代码如下:
/* 系统名: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 //倒序排列  }  }  } 
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部