Small example of sort page sorting and paging

  • 2020-05-10 18:40:36
  • OfStack


/*  System name: SaleManage
*  Module name: SortPags
*  Module description: sort paging class ( The incoming DataTable, And related information , Then the paging , And sorted )
*  Developers: Peter Luo
*  Development time: 2012 years 4 month 6 day 
*/
using System;
using System.Collections.Generic;
using System.Linq;
 using System.Text;
 using System.Data ;

 namespace Sale_Core
 {
 public class SortPags
 {
 /// 
 ///  Store the incoming data 
 /// 
 private DataTable _DtSource = null;
 private DataView _DvSource = null;

 /// 
 ///  Paging sort class 
 /// 
 ///  The data source to be paged or sorted 
 public SortPags(DataTable dt)
 {
 this._DtSource = dt;
 }

 /// 
 ///  Paging sort class 
 /// 
 ///  The data source to be paged or sorted 
 public SortPags(DataView dv)
 {
 this._DvSource = dv;
 }

 /// 
 ///  The total number of pages 
 /// 
 private int _PageCount;

 /// 
 ///  Number of records per page 
 /// 
 private int _PageSiz;

 /// 
 ///  The total number of records 
 /// 
 private int _RowCount;

 /// 
 ///  Sorting type 
 /// ASC  ascending 
 /// DESC  Descending order 
 /// 
 private SortType _SortKind;

 /// 
 ///  Record the current page Index
 /// 
 private int _CurrentPageIndex;

 /// 
 ///  The data source 
 /// 
 public DataTable DtSource
 {
 get
 {
 return _DtSource;
 }
 }

 /// 
 ///  The page number 
 /// 
 public int PageCount
 {
 get
 {
 return _PageCount;
 }
 }

 /// 
 ///  Page display quantity 
 /// 
 public int PageSize
 {
 get
 {
 return _PageSiz;
 }
 set
 {
 _PageSiz = value;
 }
 }

 /// 
 ///  Read only, not write, get the total number of data from the data source 
 /// 
 public int RowCount
 {
 get
 {
 return _RowCount;
 }
 }

 public SortType SortKind
 {
 get
 {
 return _SortKind;
 }
 set
 {
 _SortKind = value;
 }
 }

 /// 
 ///  Record the current page Index
 /// 
 public int CurrentPageIndex
 {
 get
 {
 return _CurrentPageIndex;
 }
 }

 public DataView Sort(string sortName, SortType sortKind)
 {
 return new DataView();
 }

 /// 
 ///  Gets the render page paged by the given field, (sort - > Paging) 
 /// 
 ///  Pass in the sorted fields 
 ///  Sort type :SortType.ASC  ascending  SortType.DESC  Descending order 
 ///  Page size (the number of records to display on the page) 
 ///  Current page index
 /// 
 public DataTable GetCurrentPageSortByFileName(string sortName, SortType sortKind, int pageSize, int currentPageIndex)
 {
 if (pageSize == 0)
 return DtSource;// If not filled in pagesize So return the entire data source 
 if (currentPageIndex <= 0)
 return DtSource; // If no current page is passed in index, Returns the entire data source 
 if (sortName == "")
 return GetCurrentPage(pageSize, currentPageIndex);// If the sort field is not written, there is only paging and no sorting 

 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) // If you calculate the number of pages * The amount of data on the page is less than the number of records, then the page size is automatically +1
 {
 _PageCount++;
 }

 int currentBeginRowIndex = pageSize * (currentPageIndex - 1); // The beginning line of the current page 
 int currentEndRowIndex = pageSize * currentPageIndex - 1;// The end line of the current page  
 DataTable dtRes = _DtSource.Clone(); // Replicate the data source table structure  
 for (int i = currentBeginRowIndex; i <= currentEndRowIndex; i++) // Copy the data of the current page to the new one datatable In the 
 {
 if (i >= DtSource.Rows.Count) 
 break; // When the record of the current page is smaller than the record that should be displayed on the page, only the data in the current page is taken 
 DataRow dr = dtRes.NewRow();
 for (int j = 0; j < _DtSource.Columns.Count; j++)
 {
 dr[j] = dv[i][j];
 }
 dtRes.Rows.Add(dr);
 }
 return dtRes;
 }

 /// 
 /// 
 /// 
 ///  Size per page (number of records per page) 
 ///  Current page index 
 /// 
 public DataTable GetCurrentPage(int pageSize, int currentPageIndex)
 {
 if (pageSize ==0)
 {
 return DtSource;// If not filled in pagesize So return the entire data source 
 }
 if (currentPageIndex <= 0)
 {
 return DtSource;// If no current page is passed in index, Returns the entire data source 
 }
 _PageSiz = pageSize;

 _CurrentPageIndex = currentPageIndex;
 this._RowCount = this.DtSource.Rows.Count;
 this._PageCount = this.RowCount / this.PageSize;
 if (_PageCount * PageSize < RowCount) // If you calculate the number of pages * The amount of data on the page is less than the number of records, then the page size is automatically +1
 _PageCount++;
 int CurrentBeginRowIndex = PageSize * (currentPageIndex - 1); // The beginning line of the current page 
 int CurrentEndRowIndex = PageSize * currentPageIndex - 1; // The end line of the current page  
 DataView dv;
 if (_DvSource == null)
 dv = new DataView(DtSource);
 else
 dv = _DvSource;
 DataTable dtRes = _DtSource.Clone(); // Replicate the data source table structure 
 for (int i = CurrentBeginRowIndex; i <= CurrentEndRowIndex; i++) // Copy the data of the current page to the new one datatable In the 
 {
 if (i >= DtSource.Rows.Count) break; // When the record of the current page is smaller than the record that should be displayed on the page, only the data in the current page is taken 
 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, // Ascending order  
 DESC // Arranged in reverse chronological order 
 }
 }
 }  

Related articles: