/// <summary> ///DataTable Paging and fetching data for specified page numbers/// </summary> /// <param name= "Dtall" >DataTable</param> /// <param name= "PageNo" >page number, note: Starting from 1</param> /// <param name= "PageSize" >number of bars per page</param> /// <returns>DataTable data with specified page numbers</returns> PrivateDataTable Getonepagetable (DataTable Dtall,intPageNo,intpageSize) { varTotalCount =DtAll.Rows.Count; varTotalpage =gettotalpage (TotalCount, pageSize); varCurrentPage =PageNo; CurrentPage= (CurrentPage > Totalpage totalpage:currentpage);//if the pageno is too large, it is more positive pageno=pagecountCurrentPage = (CurrentPage <=0?1: CurrentPage);//if pageno<=0, then change to homepage//----Clone table structure to a new table varOnepagetable =Dtall.clone (); //----Remove 1 pages of data to a new table varRowbegin = (CurrentPage-1) *pageSize; varRowend = CurrentPage *pageSize; Rowend= (Rowend > totalcount?)totalcount:rowend); for(vari = Rowbegin; I <= rowend-1; i++) { varNewRow =Onepagetable.newrow (); varOldrow =Dtall.rows[i]; foreach(DataColumn columninchdtall.columns) {newrow[column. ColumnName]=Oldrow[column. ColumnName]; } onePageTable.Rows.Add (NewRow); } returnonepagetable; } /// <summary> ///returns the total number of pages after paging/// </summary> /// <param name= "TotalCount" >total number of records</param> /// <param name= "PageSize" >show number of bars per page</param> /// <returns>Total Pages</returns> Public intGettotalpage (intTotalCount,intpageSize) { varTotalpage = (totalcount/pagesize) + (totalcount% PageSize >0?1:0); returnTotalpage; }
c#-datatable Pagination Code