sort page 排序和分頁的小例子
更新時間:2013年05月08日 11:05:10 作者:
花了一上午時間,終于寫了一個進行排序并且分頁的類,下面將代碼貼出來,望大家指正。
復制代碼 代碼如下:
/* 系統(tǒng)名:SaleManage
* 模塊名:SortPags
* 模塊說明:排序分頁類(傳入DataTable,及相關(guān)信息,然后分頁,并排序)
* 開發(fā)者:Peter Luo
* 開發(fā)時間:2012年4月6日
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data ;
namespace Sale_Core
{
public class SortPags
{
///
/// 存儲傳入的數(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;
///
/// 記錄當前頁面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;
}
}
///
/// 記錄當前頁面Index
///
public int CurrentPageIndex
{
get
{
return _CurrentPageIndex;
}
}
public DataView Sort(string sortName, SortType sortKind)
{
return new DataView();
}
///
/// 獲取按照給定字段分頁后的制定頁,(排序->分頁)
///
/// 傳入排序的字段
/// 排序的類型:SortType.ASC 升序 SortType.DESC 降序
/// 頁面的大小(頁面內(nèi)要顯示的記錄的數(shù)量)
/// 當前頁面的index
///
public DataTable GetCurrentPageSortByFileName(string sortName, SortType sortKind, int pageSize, int currentPageIndex)
{
if (pageSize == 0)
return DtSource;//如果沒有填寫pagesize那么返回整個數(shù)據(jù)源
if (currentPageIndex <= 0)
return DtSource; //如果沒有傳入當前頁面index,則返回整個數(shù)據(jù)源
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) //如果計算出的頁面數(shù)*頁面上的數(shù)據(jù)量小于記錄數(shù),那么頁面大小自動+1
{
_PageCount++;
}
int currentBeginRowIndex = pageSize * (currentPageIndex - 1); //當前頁面的開始行
int currentEndRowIndex = pageSize * currentPageIndex - 1;//當前頁面的結(jié)束行
DataTable dtRes = _DtSource.Clone(); //復制數(shù)據(jù)源表結(jié)構(gòu)
for (int i = currentBeginRowIndex; i <= currentEndRowIndex; i++) //復制當前頁面的數(shù)據(jù)到新的datatable中
{
if (i >= DtSource.Rows.Count)
break; //當前頁面的記錄小于該頁面應該顯示的記錄時,就只取當前頁面中的數(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;
}
///
///
///
/// 每頁面大?。總€頁面上記錄的數(shù)量)
/// 當前頁面索引
///
public DataTable GetCurrentPage(int pageSize, int currentPageIndex)
{
if (pageSize ==0)
{
return DtSource;//如果沒有填寫pagesize那么返回整個數(shù)據(jù)源
}
if (currentPageIndex <= 0)
{
return DtSource;//如果沒有傳入當前頁面index,則返回整個數(shù)據(jù)源
}
_PageSiz = pageSize;
_CurrentPageIndex = currentPageIndex;
this._RowCount = this.DtSource.Rows.Count;
this._PageCount = this.RowCount / this.PageSize;
if (_PageCount * PageSize < RowCount) //如果計算出的頁面數(shù)*頁面上的數(shù)據(jù)量小于記錄數(shù),那么頁面大小自動+1
_PageCount++;
int CurrentBeginRowIndex = PageSize * (currentPageIndex - 1); //當前頁面的開始行
int CurrentEndRowIndex = PageSize * currentPageIndex - 1; //當前頁面的結(jié)束行
DataView dv;
if (_DvSource == null)
dv = new DataView(DtSource);
else
dv = _DvSource;
DataTable dtRes = _DtSource.Clone(); //復制數(shù)據(jù)源表結(jié)構(gòu)
for (int i = CurrentBeginRowIndex; i <= CurrentEndRowIndex; i++) //復制當前頁面的數(shù)據(jù)到新的datatable中
{
if (i >= DtSource.Rows.Count) break; //當前頁面的記錄小于該頁面應該顯示的記錄時,就只取當前頁面中的數(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# 利用反射根據(jù)類名創(chuàng)建類的實例對象
這篇文章主要介紹了詳解C# 利用反射根據(jù)類名創(chuàng)建類的實例對象,“反射”其實就是利用程序集的元數(shù)據(jù)信息,感興趣的小伙伴們可以參考一下。2017-03-03C#根據(jù)前臺傳入實體名稱實現(xiàn)動態(tài)查詢數(shù)據(jù)
這篇文章主要為大家詳細介紹了C#如何根據(jù)前臺傳入實體名稱實現(xiàn)動態(tài)查詢數(shù)據(jù)的功能,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2023-04-04C# 啟用事務(wù)提交多條帶參數(shù)的SQL語句實例代碼
這篇文章主要介紹了C# 啟用事務(wù)提交多條帶參數(shù)的SQL語句實例代碼,需要的朋友可以參考下2018-02-02