sort page 排序和分页的小例子
发布时间:2022-07-31 23:08:37
标签:sort,page
/* 系统名: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 //倒序排列
}
}
}


猜你喜欢
- 需求读200+的CSV/EXCEL文件,按文件名称存到不同数据库前期准备环境maven + jdk8 + mysql代码展示pom文件<
- 一、点名器需求:我有一个文件里面存储了班级同学的姓名,每一个姓名占一行,要求通过程序实现随机点名器实现步骤:创建字符缓冲输入流对象创建Arr
- 目录批量更新同一张表的数据更新多条数据,每条数据都不一样java中for循环实现方式一条SQL,服务端逐条更新mybatis实现方式使用Fl
- 大多数的B2C商城项目都会有限时活动,当用户下单后都会有支付超时时间,当订单超时后订单的状态就会自动变成已取消 ,这个功能的实现
- 这里主要是总结一下如何监听有未接来电的问题 1.1 使用广播 * BrocastReceiver实现思路 : 静态注册监听and
- 首先给大家声明一点:需要 jdk 7 , tomcat需要支持websocket的版本 1.InitServlet &n
- Eureka注册的服务之间互相调用1.请求方启动类添加注解,扫描Eureka 中的全部服务@SpringBootApplication@En
- 前言安卓开发中一个很基础的操作就是打开一个 Activity ,另一个很必要的操作就是,打开一个 Activity ,在打开的 Activi
- 本文实例为大家分享了java批量解析微信dat文件的具体代码,供大家参考,具体内容如下微信图片默认路径:C:\Users\b-eet\Doc
- 实现功能实现使用FTP上传、下载、重命名、刷新、删除功能开发环境开发工具: Visual Studio 2013.NET Framework
- 使用的是 idea - Lifecycle-package 的方式打包(maven)确认 <packaging>wa
- 本文实例讲述了Android编程实现仿QQ发表说说,上传照片及弹出框效果。分享给大家供大家参考,具体如下:代码很简单,主要就是几个动画而已,
- public static string Escape(string s) &nb
- 程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。#include <s
- Spring Security支持在响应中添加各种安全头默认相应安全头:Cache-Control: no-cache, no-store,
- 引言上一节讲面试中被问到分布式系统概念相关的,讲完了分布式系统的概念,优点缺点和 RPC 后,我以为这个问题就到此结束了,没想到成功给自己挖
- 1、CountDownLatch:一个同步工具类,它允许一个或多个线程一直等待,直到其他线程的操作执行完后再执行。2、ThreadPoolE
- 各个方法1. 得到class的成员变量首先得到object的class对象然后在class对象中用getDeclaredFields()方法
- 1.定义字符串字符串常见的构造方式如下:String s1 = "with";String s2 = new Strin
- 效果和代码都非常直观:实例1:TimePicker<RelativeLayout xmlns:android="http:/