手机
当前位置:查字典教程网 >编程开发 >C#教程 >sort page 排序和分页的小例子
sort page 排序和分页的小例子
摘要:复制代码代码如下:/*系统名:SaleManage*模块名:SortPags*模块说明:排序分页类(传入DataTable,及相关信息,然后...

复制代码 代码如下:

/* 系统名: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 排序和分页的小例子】相关文章:

C#修改MAC地址类的实例

c# 删除所有的空文件夹的小例子

c#进度条 progressBar 使用方法的小例子

C#定位txt指定行的方法小例子

C# 图片剪切与缩小的实例

gridview 显示图片的实例代码

C# Bitmap 复制的小例子

c# n个数排序实现代码

C#身份证验证小例子

c# 开机启动项的小例子

精品推荐
分类导航