SoFunction
Updated on 2025-03-07

C# DataTable paging processing instance code

Sometimes when the amount of data we get from the database is too large and we don’t need to display so much at once, we have to paginate the data and let each page display different data.

public DataTable GetPagedTable(DataTable dt, int PageIndex, int PageSize)//PageIndex represents the page, and PageSize represents the number of records per page  {
   if (PageIndex == 0)
    return dt;// Page 0 represents data per page, return directly
   DataTable newdt = ();
   ();//copy dt's framework
   int rowbegin = (PageIndex - 1) * PageSize;
   int rowend = PageIndex * PageSize;

   if (rowbegin >= )
    return newdt;//The number of source data records is less than or equal to the record to be displayed, and directly return to dt
   if (rowend > )
    rowend = ;
   for (int i = rowbegin; i <= rowend - 1; i++)
   {
    DataRow newdr = ();
    DataRow dr = [i];
    foreach (DataColumn column in )
    {
     newdr[] = dr[];
    }
    (newdr);
   }
   return newdt;
  }

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.