C# 分页类

using System;
using System.Data;
using System.Windows.Forms;

namespace ClothLib.DataAccess
{
 /// <summary>
 /// paging 的摘要说明。
 /// </summary>
 public class paging
 {
  int PageCount;
  int maxRec;
  int pageSize;
  public static int currentPage;
  int recNo;

  

  DataTable dtSource;
  private System.Windows.Forms.DataGrid grid;
  string lblstr;//输入第几页的文本
  int count;
  string table;


  public paging(DataTable dtSource,System.Windows.Forms.DataGrid grid,string lblstr,int count,string table)
  {
   this.table = table;
   this.dtSource = dtSource;
   this.grid = grid;
   this.lblstr = lblstr;
   this.count = count;

   //
   // TODO: 在此处添加构造函数逻辑
   //
  }

  
  //显示当前页数的方法
  public void DisplayPageInfo()
  {
   this.lblstr = "第" + currentPage.ToString() + "页" ;
  }

  //判断显示数量文本框中是否输入的值
  public bool CheckFillButton()
  {
   
   // Check if the user clicks the "Fill Grid" button.
   string strpageSize = this.count.ToString();
   if(strpageSize.Equals(""))
   {
    pageSize = 0 ;
   }
   else
   {
    pageSize = int.Parse(this.count.ToString());
   }
   if (pageSize == 0)
   {
    MessageBox.Show("请设置第页显示数据的行数!");
    return false;
   }
   else
   {
    return true;
   }
  }

  public void LoadPage()
  {
   int i;
   int startRec;
   int endRec;
   DataTable dtTemp;

   //Clone the source table to create a temporary table.
   dtTemp = dtSource.Clone();

   if (currentPage == PageCount && currentPage != this.dtSource.Rows.Count/int.Parse(this.count.ToString()))
   {
    endRec = maxRec;
   }
   else
   {
    CheckFillButton();
    recNo = pageSize * currentPage - int.Parse(this.count.ToString());
    if( this.dtSource.Rows.Count>pageSize * currentPage )
    {
     endRec = pageSize * currentPage;
    }
    else
    {
     endRec = this.dtSource.Rows.Count;
    }   
   }
   startRec = recNo;

   //Copy rows from the source table to fill the temporary table.
   for (i = startRec; i < endRec; i++)
   {
    dtTemp.ImportRow(dtSource.Rows[i]);
    recNo += 1;
   }
   this.dtSource = dtTemp;
   this.grid.DataSource = dtTemp;
   DisplayPageInfo();
  }

  

  //显示首页的方法
  public void FristPage()
  {
   if (CheckFillButton() == false)
  {
   return;
  }

   //Check if you are already at the first page.
   if (currentPage == 1)
  {
   MessageBox.Show("已经是第一页了!");
   return;
  }

   currentPage = 1;
   recNo = 0;
   LoadPage();
  }


  //显示上一页的方法
  public void PreviousPage()
  {
   if (CheckFillButton() == false)
   {
    return;
   }

   if (currentPage == PageCount)
   {
    recNo = pageSize * (currentPage - 2);
   }

   currentPage -= 1;
   //Check if you are already at the first page.
   if (currentPage < 1)
   {
    MessageBox.Show("You are at the First Page!");
    currentPage = 1;
    return;
   }
   else
   {
    recNo = pageSize * (currentPage - 1);
   }
   LoadPage();
  }

  //显示下一页的方法
  public void Nextpage()
  {
   if (CheckFillButton() == false)
   {
    return;
   }

   //Check if the user clicks the "Fill Grid" button.
   if (pageSize == 0)
   {
    MessageBox.Show("Set the Page Size, and then click the Fill Grid button!");
    return;
   }

   currentPage += 1;
   if (currentPage >= this.dtSource.Rows.Count/int.Parse(this.count.ToString()))
   {
    //currentPage = PageCount;
    //Check if you are already at the last page.
    
    MessageBox.Show("已经是最后页了!");
    currentPage  = currentPage - 1;
    return;
   
   }
   LoadPage();
  }

  //显示最后一页的方法
  public void Lastpage()
  {
   if (CheckFillButton() == false)
   {
    return;
   }

   PageCount = this.dtSource.Rows.Count / int.Parse(this.count.ToString());
   currentPage = PageCount;
   recNo = pageSize * (currentPage - 1);
   LoadPage();
  }
 } 
}
 

阅读更多
换一批

没有更多推荐了,返回首页