永恆的話題--分頁

 

 //  獲取指定頁。
  public static DataSet GetDsPage(int npgNo,int npgSize,DataSet ds,ref int npgCount,ref int nRecordCount)
  {    //參數依次意思爲 當前頁,    每頁記錄數,                    總共頁數,        總共記錄數
   //PgSize小於1,則不分頁
   
   if (npgSize < 1 || ds == null || ds.Tables.Count < 1)
   {
    return ds;
   }
   nRecordCount = ds.Tables[0].Rows.Count;

   int npgStart = (npgNo - 1) * npgSize ;
   //每一頁的開始記錄的位置
   int npgEnd =npgStart + npgSize - 1;
   //每一頁的結束記錄的位置
   int nCount = ds.Tables[0].Rows.Count ;
   
   //計算總共頁數
   npgCount = (int)(nCount / npgSize); 
   if((nCount % npgSize) > 0)
   {
    npgCount = npgCount + 1;
   }
   //計算總共頁數
   
   if(npgStart > nCount)
   {
    return null ;
   }

   //到達最後一頁則總共記錄數爲 結束記錄數
   if(npgEnd > nCount)
   {
    npgEnd = nCount ;
   }
   //去掉前面不必要的記錄
   for(int i = 0;i < npgStart;i++)
   {
    ds.Tables[0].Rows.RemoveAt(0);
   }
   //去掉後面不必要的記錄
   for(int j = 1;j < nCount - npgEnd;j++)
   {    
    ds.Tables[0].Rows.RemoveAt(npgSize);
   }  
   return ds;
  } 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章