- 精品下载 | 实用查询 | 词典查询 | 桌面壁纸 | 网址 | 笑话 | FLASH频道 | 天气文章资讯 | 站长工具 | 证件办理 | 闪字生成 | 广告代码 | 在线手册 | 有问必答
您现在的位置: 蓝派网 >> 文章中心 >> 网络编程 >> .NET >> c#(Csharp) >> 正文
站内文章搜索:           

winfrom中的datagrid的翻页类

作者:佚名    文章来源:网络    更新时间 :2007-5-30 15:40:19
using System;
using System.Windows.Forms;
using System.Data;
namespace HRMS.Common
{
/// <summary>
/// HRDataGrid 的摘要说明。
/// </summary>
public class HRDataGrid:DataGrid
{
  private void InitializeComponent()
  {
   ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
   ((System.ComponentModel.ISupportInitialize)(this)).EndInit();

  }

  public HRDataGrid()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
  }
  /// <summary>
  /// 总页数
  /// </summary>
  private int pageCount=1;
  /// <summary>
  /// 每页行数
  /// </summary>
  private int pageRows=40;
  /// <summary>
  /// 总行数
  /// </summary>
  private int rowCount=1;
  /// <summary>
  /// 当前页
  /// </summary>
  private int curentPageIndex=0;
  /// <summary>
  /// 能否前进
  /// </summary>
  private bool canPri=false;
  /// <summary>
  /// 能否后退
  /// </summary>
  private bool canNext=false;

  /// <summary>
  /// 邦订表
  /// </summary>
  private DataTable buildDataTable=new DataTable();

  /// <summary>
  /// 邦订对应的表
  /// </summary>
  /// <param name="dt"></param>
  public void SetHRDataSource(DataTable dt)
  {
   this.buildDataTable=dt;
   this.rowCount=dt.Rows.Count;
   this.pageCount=rowCount/pageRows;
   if(rowCount % pageRows>0)
   {
    this.pageCount=this.pageCount+1;
   }
   if(pageCount<1)
   {
    PageCount=1;
   }
   if(pageCount>curentPageIndex)
   {
    this.canNext=true;
   }
   else
   {
    this.canNext=false;
   }
   if(curentPageIndex>1)
   {
    this.canPri=true;
   }
   else
   {
    this.canPri=false;
   }
   
   BuildTable(this.curentPageIndex);
  }


  /// <summary>
  /// 下一页
  /// </summary>
  public void NextPage()
  {
   if(!canNext)
   {
    MessageBox.Show("已经到最后一页!");
    return;
   }
   this.canPri=true;
   this.curentPageIndex=curentPageIndex+1;
   if(curentPageIndex>=this.pageCount)
   {
    this.canNext=false;
   }
   else
   {
    this.canNext=true;
   }
   BuildTable(curentPageIndex);
  }

  /// <summary>
  /// 前一页
  /// </summary>
  public void PriPage()
  {
   if(!canPri)
   {
    MessageBox.Show("已经到最前一页!");
    return;
   }
   this.canNext=true;
   this.curentPageIndex=curentPageIndex-1;
   if(curentPageIndex<=1)
   {
    this.canPri=false;
   }
   else
   {
    this.canPri=true;
   }
   BuildTable(curentPageIndex);
  }

  private void BuildTable(int index)
  {
   if(index<1)
   {
    index=1;
   }
   if(index>this.pageCount)
   {
    index=this.pageCount;
    //throw new Exception("需要邦订的页数超出范围!");
   }
   DataTable tempTable=this.buildDataTable.Copy();
   tempTable.Clear();
   int start=(index-1)*pageRows;
   int end=index*pageRows;
   if(end>this.rowCount)
   {
    end=this.rowCount;
   }
   for(int intx=start;intx<end;intx++)
   {
    DataRow dr=tempTable.NewRow();
    dr.ItemArray=((DataRow)(this.buildDataTable.Rows[intx])).ItemArray;
    tempTable.Rows.Add(dr);
   }
   this.DataSource=tempTable;
  }

  public bool CanPri
  {
   set
   {
    this.canPri=value;
   }
   get
   {
    return this.canPri;
   }
  }
  public bool CanNext
  {
   set
   {
    this.canNext=value;
   }
   get
   {
    return this.canNext;
   }
  }

  public int CurentPageIndex
  {
   set
   {
    this.curentPageIndex=value;
   }
   get
   {
    return this.curentPageIndex;
   }
  }

  public int RowCount
  {
   set
   {
    this.rowCount=value;
   }
   get
   {
    return this.rowCount;
   }
  }

  public int PageCount
  {
   set
   {
    this.pageCount=value;
   }
   get
   {
    return this.pageCount;
   }
  }


  public int PageRows
  {
   set
   {
    this.pageRows=value;
   }
   get
   {
    return this.pageRows;
   }
  }

  public DataTable BuildDataTable
  {
   set
   {
    this.buildDataTable=value;
   }
   get
   {
    return this.buildDataTable;
   }
  }
}
}




调用这个类:
//Set dataTable
public void BuildingGridData(DataSet ds)
  {
      this.dataGrid_person.SetHRDataSource(ds.Tables[0]);
   this.toolBarButton_priPage.Enabled=dataGrid_person.CanPri;
   this.toolBarButton_nextPage.Enabled=dataGrid_person.CanNext;
   this.statusBarPanel_GrifInfo.Text="第"+dataGrid_person.CurentPageIndex.ToString()+"页/共"+dataGrid_person.PageCount.ToString()+"页";
   this.statusBarPanel_GridRows.Text="共"+dataGrid_person.RowCount.ToString()+"笔记录";
  }

//点按上一页
this.dataGrid_person.PriPage();
     this.toolBarButton_priPage.Enabled=dataGrid_person.CanPri;
     this.toolBarButton_nextPage.Enabled=dataGrid_person.CanNext;

//点按下一页
this.dataGrid_person.NextPage();
     this.toolBarButton_priPage.Enabled=dataGrid_person.CanPri;
     this.toolBarButton_nextPage.Enabled=dataGrid_person.CanNext;



  • 上一篇文章:
  • 下一篇文章:
  • 百度搜索: winfrom中的datagrid的翻页类

  • 发表评论】【打印此文】【关闭窗口】【点击数:
    ★好玩的休闲小游戏★
    网友评论:
    数据载入中,请稍后……