sort page 排序和分页的小例子
时间:2022-07-31 23:08:37
/* 系统名: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 //倒序排列
}
}
}
标签:sort,page
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
c#语言使用Unity粒子系统制作手雷爆炸
2021-10-11 11:13:46
![](https://img.aspxhome.com/file/2023/8/92918_0s.png)
MyBatis 动态拼接Sql字符串的问题
2021-08-09 05:23:46
java编程之基于SpringBoot框架实现扫码登录
2023-02-14 02:39:28
![](https://img.aspxhome.com/file/2023/9/62999_0s.png)
Java基础教程之理解Annotation详细介绍
2022-02-28 10:07:12
详解Java String字符串获取每一个字符及常用方法
2022-12-14 05:15:33
![](https://img.aspxhome.com/file/2023/6/85486_0s.jpg)
Java反射(Class类,Class对象获取)
2021-06-27 05:55:24
![](https://img.aspxhome.com/file/2023/4/63444_0s.png)
Java的枚举类型使用方法详解
2023-11-19 04:32:55
Spring Cloud 请求重试机制核心代码分析
2023-04-11 04:00:23
Netty分布式从recycler对象回收站获取对象过程剖析
2021-08-12 06:45:48
Spring boot中PropertySource注解的使用方法详解
2022-07-30 02:29:57
![](https://img.aspxhome.com/file/2023/9/72539_0s.jpg)
关于Mybatis-Plus Wrapper是否应该出现在Servcie类中
2023-11-28 22:04:56
Mybatis-plus基于redis实现二级缓存过程解析
2021-12-08 23:34:43
![](https://img.aspxhome.com/file/2023/1/63151_0s.png)
Java实现平滑加权轮询算法之降权和提权详解
2022-10-02 20:51:22
Java面试必备八股文整理
2023-11-29 12:03:50
![](https://img.aspxhome.com/file/2023/8/59068_0s.jpg)
Java对世界不同时区timezone之间时间转换的处理方法
2023-11-08 13:19:30
![](https://img.aspxhome.com/file/2023/0/72640_0s.jpg)
使用logback屏蔽一些包的日志
2023-08-08 20:46:20
Swagger注解-@ApiModel和@ApiModelProperty的用法
2023-02-05 23:57:48
Visual C#中如何使用IComparable和IComparer接口
2021-12-17 20:01:02
Springboot整合MybatisPlus的实现过程解析
2021-06-14 02:47:06
Java cookie和session会话技术介绍
2021-12-30 06:51:22