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();
  }
 } 
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值