sort page 排序和分頁的小例子
更新時(shí)間:2013年05月08日 11:05:10 作者:
花了一上午時(shí)間,終于寫了一個(gè)進(jìn)行排序并且分頁的類,下面將代碼貼出來,望大家指正。
復(fù)制代碼 代碼如下:
/* 系統(tǒng)名:SaleManage
* 模塊名:SortPags
* 模塊說明:排序分頁類(傳入DataTable,及相關(guān)信息,然后分頁,并排序)
* 開發(fā)者:Peter Luo
* 開發(fā)時(shí)間:2012年4月6日
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data ;
namespace Sale_Core
{
public class SortPags
{
///
/// 存儲(chǔ)傳入的數(shù)據(jù)
///
private DataTable _DtSource = null;
private DataView _DvSource = null;
///
/// 分頁排序類
///
/// 要分頁或排序的數(shù)據(jù)源
public SortPags(DataTable dt)
{
this._DtSource = dt;
}
///
/// 分頁排序類
///
/// 要分頁或排序的數(shù)據(jù)源
public SortPags(DataView dv)
{
this._DvSource = dv;
}
///
/// 頁面總數(shù)
///
private int _PageCount;
///
/// 每頁記錄數(shù)量
///
private int _PageSiz;
///
/// 記錄總數(shù)
///
private int _RowCount;
///
/// 排序類型
/// ASC 升序
/// DESC 降序
///
private SortType _SortKind;
///
/// 記錄當(dāng)前頁面Index
///
private int _CurrentPageIndex;
///
/// 數(shù)據(jù)源
///
public DataTable DtSource
{
get
{
return _DtSource;
}
}
///
/// 頁面數(shù)量
///
public int PageCount
{
get
{
return _PageCount;
}
}
///
/// 頁面顯示數(shù)量
///
public int PageSize
{
get
{
return _PageSiz;
}
set
{
_PageSiz = value;
}
}
///
/// 只讀、不能寫,獲取該數(shù)據(jù)源那數(shù)據(jù)總數(shù)
///
public int RowCount
{
get
{
return _RowCount;
}
}
public SortType SortKind
{
get
{
return _SortKind;
}
set
{
_SortKind = value;
}
}
///
/// 記錄當(dāng)前頁面Index
///
public int CurrentPageIndex
{
get
{
return _CurrentPageIndex;
}
}
public DataView Sort(string sortName, SortType sortKind)
{
return new DataView();
}
///
/// 獲取按照給定字段分頁后的制定頁,(排序->分頁)
///
/// 傳入排序的字段
/// 排序的類型:SortType.ASC 升序 SortType.DESC 降序
/// 頁面的大?。撁鎯?nèi)要顯示的記錄的數(shù)量)
/// 當(dāng)前頁面的index
///
public DataTable GetCurrentPageSortByFileName(string sortName, SortType sortKind, int pageSize, int currentPageIndex)
{
if (pageSize == 0)
return DtSource;//如果沒有填寫pagesize那么返回整個(gè)數(shù)據(jù)源
if (currentPageIndex <= 0)
return DtSource; //如果沒有傳入當(dāng)前頁面index,則返回整個(gè)數(shù)據(jù)源
if (sortName == "")
return GetCurrentPage(pageSize, currentPageIndex);//如果排序字段沒寫,則只有分頁,不進(jìn)行排序
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) //如果計(jì)算出的頁面數(shù)*頁面上的數(shù)據(jù)量小于記錄數(shù),那么頁面大小自動(dòng)+1
{
_PageCount++;
}
int currentBeginRowIndex = pageSize * (currentPageIndex - 1); //當(dāng)前頁面的開始行
int currentEndRowIndex = pageSize * currentPageIndex - 1;//當(dāng)前頁面的結(jié)束行
DataTable dtRes = _DtSource.Clone(); //復(fù)制數(shù)據(jù)源表結(jié)構(gòu)
for (int i = currentBeginRowIndex; i <= currentEndRowIndex; i++) //復(fù)制當(dāng)前頁面的數(shù)據(jù)到新的datatable中
{
if (i >= DtSource.Rows.Count)
break; //當(dāng)前頁面的記錄小于該頁面應(yīng)該顯示的記錄時(shí),就只取當(dāng)前頁面中的數(shù)據(jù)
DataRow dr = dtRes.NewRow();
for (int j = 0; j < _DtSource.Columns.Count; j++)
{
dr[j] = dv[i][j];
}
dtRes.Rows.Add(dr);
}
return dtRes;
}
///
///
///
/// 每頁面大小(每個(gè)頁面上記錄的數(shù)量)
/// 當(dāng)前頁面索引
///
public DataTable GetCurrentPage(int pageSize, int currentPageIndex)
{
if (pageSize ==0)
{
return DtSource;//如果沒有填寫pagesize那么返回整個(gè)數(shù)據(jù)源
}
if (currentPageIndex <= 0)
{
return DtSource;//如果沒有傳入當(dāng)前頁面index,則返回整個(gè)數(shù)據(jù)源
}
_PageSiz = pageSize;
_CurrentPageIndex = currentPageIndex;
this._RowCount = this.DtSource.Rows.Count;
this._PageCount = this.RowCount / this.PageSize;
if (_PageCount * PageSize < RowCount) //如果計(jì)算出的頁面數(shù)*頁面上的數(shù)據(jù)量小于記錄數(shù),那么頁面大小自動(dòng)+1
_PageCount++;
int CurrentBeginRowIndex = PageSize * (currentPageIndex - 1); //當(dāng)前頁面的開始行
int CurrentEndRowIndex = PageSize * currentPageIndex - 1; //當(dāng)前頁面的結(jié)束行
DataView dv;
if (_DvSource == null)
dv = new DataView(DtSource);
else
dv = _DvSource;
DataTable dtRes = _DtSource.Clone(); //復(fù)制數(shù)據(jù)源表結(jié)構(gòu)
for (int i = CurrentBeginRowIndex; i <= CurrentEndRowIndex; i++) //復(fù)制當(dāng)前頁面的數(shù)據(jù)到新的datatable中
{
if (i >= DtSource.Rows.Count) break; //當(dāng)前頁面的記錄小于該頁面應(yīng)該顯示的記錄時(shí),就只取當(dāng)前頁面中的數(shù)據(jù)
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 //倒序排列
}
}
}
相關(guān)文章
C#中面向?qū)ο缶幊虣C(jī)制之繼承學(xué)習(xí)筆記
這篇文章主要介紹了C#中面向?qū)ο缶幊虣C(jī)制之繼承學(xué)習(xí)筆記,本文給出一個(gè)簡(jiǎn)單子實(shí)例講解C#中的繼承,并講解了一些C#繼承的知識(shí)技巧,需要的朋友可以參考下2015-01-01詳解C# 利用反射根據(jù)類名創(chuàng)建類的實(shí)例對(duì)象
這篇文章主要介紹了詳解C# 利用反射根據(jù)類名創(chuàng)建類的實(shí)例對(duì)象,“反射”其實(shí)就是利用程序集的元數(shù)據(jù)信息,感興趣的小伙伴們可以參考一下。2017-03-03C# [ImportDll()] 知識(shí)小結(jié)
今天小編就為大家分享一篇關(guān)于C# [ImportDll()] 知識(shí)小結(jié),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01C#根據(jù)前臺(tái)傳入實(shí)體名稱實(shí)現(xiàn)動(dòng)態(tài)查詢數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了C#如何根據(jù)前臺(tái)傳入實(shí)體名稱實(shí)現(xiàn)動(dòng)態(tài)查詢數(shù)據(jù)的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-04-04C# 啟用事務(wù)提交多條帶參數(shù)的SQL語句實(shí)例代碼
這篇文章主要介紹了C# 啟用事務(wù)提交多條帶參數(shù)的SQL語句實(shí)例代碼,需要的朋友可以參考下2018-02-02