数据分页封装类

//注意:要想让DataGridView分页显示记录,最关键的需要设置的分页类的三个属性是: 

//SetDataGridView   该属性用于设置窗体上要分页显示记录的DataGridView控件 

//RowsPerPage 该属性用来设置每页需要显示的记录数 

//SetDataView 该属性用来设置需要在DataGridView空间上显示的DataView 

//可以灵活地设置这三个属性,以满足不同的DataGridView对不同的DataView进行指定每页记录数的显示(是不是很方便?)现在举例如何设置这三个属性: 

//先获得数据源dt 

//             DataTable dt = DataBiz.GetGmsystemReplyInfo(userid,   out strErrmsg); 

//             FormPager dgvPage = new FormPager(); 

//             dgvPage.GetDataGridView = this.dataGridView1;     //需要分页的是 dataGridView1 

//             dgvPage.RowsPerPage = 10; //每页显示10条记录 

//             //获取需要分页显示的DataView             

//             dgvPage.SetDataView = dt.DefaultView; 

//             dgvPage.Paging();     //开始分页 调用分页类的Paging 方法 



//要看第一页,调用分页类的GoFirstPage方法 

//要看下一页,调用 GoNextPage方法 

//要看前一页,调用GoPrevPage方法 

//要看最后一页,调用GoLastPage方法 

//要看指定页号的页,调用 GoNoPage方法 

//只要灵活使用好了这个类,你的DataGridView控件的分页功能绝不成问题。而且随时都能了解当前到了第几页(curPage()+1),一共有多少页(TotalPage()) 

//

//注意事项,由于本类采用一次查询数据,暂存内存分页方式,如果数据量大,5000以上,可能会影响速度,注意

//必要的时候需要后台分页查询

//



using System;

using System.Collections.Generic;

using System.Text;

using System.Data;

using System.Windows.Forms; 

namespace WWBClassLib.WinForm

{

    public class FormPager

    {

        private int _RowsPerPage;   //每页记录数 

         private int _TotalPage;     //总页数 

         private int _curPage = 0;     //当前页数 

         private DataGridView _DataGridView; //要分页的DataGridView 

         private DataView _dv;       //与需要分页显示的的DataView 

         public int RowsPerPage { get { return _RowsPerPage; } set { _RowsPerPage = value; } }       //获取与设置每页记录数 

         public int TotalPage { get { return _TotalPage; } }                             //获取总页数 

         public int curPage { get { return _curPage; } set { _curPage = value; } }                   //获取与设置当前页数 

         public DataGridView GetDataGridView { set { _DataGridView = value; } }     //设置需要分页的GetDataGridView 

         public DataView SetDataView { set { _dv = value; } }   



         public FormPager() 

         { 

           

         }

        public FormPager(DataGridView datagridview, DataView dv, int RowsPerPage) 

         { 

             _DataGridView = datagridview; 

             _dv = dv; 

             _RowsPerPage = RowsPerPage; 

         } 

         public void Paging()                 //开始分页啦 

         {   //首先判断DataView中的记录数是否足够形成多页, 

             //如果不能,那么就只有一页,且DataGridView需要显示的记录等同于“最后一页”的记录 

             if (_dv.Count <= _RowsPerPage) 

             { 

                 _TotalPage = 1; 

                 GoLastPage(); 

                 return; 

             } 

             if (_dv.Count % _RowsPerPage == 0) 

             { 

                 _TotalPage = (int)(_dv.Count / _RowsPerPage); 

             } 

             else 

             { 

                 _TotalPage = (int)(_dv.Count / _RowsPerPage) + 1; 

             }



             //如果从新加载数据,保持当前分页

             if (_curPage > 0)

             {

                 GoNoPage(_curPage);

             }

             else

             {

                 GoFirstPage();

             }

         } 

         //到第一页 

         public void GoFirstPage() 

         { 

             if (_TotalPage == 1) 

             { 

                 GoLastPage(); 

                 return; 

             } 

             _curPage = 0; 

             GoNoPage(_curPage); 

         } 

         public void GoNextPage() 

         { 

             _curPage += 1; 

             if (_curPage > _TotalPage - 1) 

             { 

                 _curPage = _TotalPage - 1; 

                 return; 

             } 

             if (_curPage == _TotalPage - 1) 

             { 

                 GoLastPage(); 

                 return; 

             } 

             GoNoPage(_curPage); 

         } 

         public void GoPrevPage() 

         {//'防止不合法的当前页号 

             _curPage -= 1; 

             if (_curPage < 0) 

             { 

                 _curPage = 0; 

                 return; 

             }

             if (_curPage == 0)

             {

                 GoFirstPage();

                 return;

             }

             GoNoPage(_curPage);

         } 

         //到最后一页 

         public void GoLastPage() 

         { 

             _curPage = _TotalPage - 1; 

             int i; 

             DataTable dt; 

             dt = _dv.ToTable().Clone(); 

             for (i = (_TotalPage - 1) * _RowsPerPage; i <= _dv.Count - 1; i++) 

             { 

                 DataRow dr = dt.NewRow(); 

                 dr.ItemArray = _dv.ToTable().Rows[i].ItemArray; 

                 dt.Rows.Add(dr); 

             } 

             _DataGridView.DataSource = dt; 

         } 

         public void GoNoPage(int PageNo) 

         { 

             _curPage = PageNo; 

             if (_curPage < 0) 

             {//防止不合法的页号 

                 return; 

             } 

             //防止页号溢出 

             if (_curPage >= _TotalPage) 

             {   //页号超出上限 

                 return; 

             } 

             if (_curPage == _TotalPage - 1) 

             { 

                 //如果页号是最后一页,就显示最后一页 

                 GoLastPage(); 

                 return; 

             } 

             DataTable dt; 

             dt = _dv.ToTable().Clone(); 

             int i; 

             for (i = PageNo * _RowsPerPage; i <= (PageNo + 1) * _RowsPerPage - 1; i++) 

             { 

                 DataRow dr = dt.NewRow(); 

                 dr.ItemArray = _dv.ToTable().Rows[i].ItemArray; 

                 dt.Rows.Add(dr); 

             } 

             _DataGridView.DataSource = dt; 

         } 



    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值