zoukankan      html  css  js  c++  java
  • C# Winform DataGridView 公共分页实现

    Demo的界面

    我利用事件委托事件,仿http://www.cnblogs.com/huyong/写的公共分页用户控件

    C#代码 复制代码 收藏代码
    1. #region  版权信息   
    2. /*---------------------------------------------------------------------*  
    3. // 项目  名称:《Winform分页控件》  
    4. // 文  件  名: Pager.cs  
    5. // 描      述: 分页控件  
    6. // 作      者:kwon yan  
    7. *----------------------------------------------------------------------*/ 
    8. #endregion   
    9.   
    10. using System;   
    11. using System.Collections.Generic;   
    12. using System.ComponentModel;   
    13. using System.Drawing;   
    14. using System.Data;   
    15. using System.Linq;   
    16. using System.Text;   
    17. using System.Windows.Forms;   
    18.   
    19. namespace HuishengFS.Controls   
    20. {   
    21.     /**/  
    22.     /// <summary>   
    23.     /// 申明委托   
    24.     /// </summary>   
    25.     /// <param name="e"></param>   
    26.     /// <returns></returns>   
    27.     public delegate int EventPagingHandler(EventPagingArg e);   
    28.     /**/  
    29.     /// <summary>   
    30.     /// 分页控件呈现   
    31.     /// </summary>   
    32.     public partial class Pager : UserControl   
    33.     {   
    34.         public Pager()   
    35.         {   
    36.             InitializeComponent();   
    37.         }   
    38.         public event EventPagingHandler EventPaging;   
    39.         /**/  
    40.         /// <summary>   
    41.         /// 每页显示记录数   
    42.         /// </summary>   
    43.         private int _pageSize = 50;   
    44.         /**/  
    45.         /// <summary>   
    46.         /// 每页显示记录数   
    47.         /// </summary>   
    48.         public int PageSize   
    49.         {   
    50.             get { return _pageSize; }   
    51.             set  
    52.             {   
    53.                 _pageSize = value;   
    54.                 GetPageCount();   
    55.             }   
    56.         }   
    57.   
    58.         private int _nMax = 0;   
    59.         /**/  
    60.         /// <summary>   
    61.         /// 总记录数   
    62.         /// </summary>   
    63.         public int NMax   
    64.         {   
    65.             get { return _nMax; }   
    66.             set  
    67.             {   
    68.                 _nMax = value;   
    69.                 GetPageCount();   
    70.             }   
    71.         }   
    72.   
    73.         private int _pageCount = 0;   
    74.         /**/  
    75.         /// <summary>   
    76.         /// 页数=总记录数/每页显示记录数   
    77.         /// </summary>   
    78.         public int PageCount   
    79.         {   
    80.             get { return _pageCount; }   
    81.             set { _pageCount = value; }   
    82.         }   
    83.   
    84.         private int _pageCurrent = 0;   
    85.         /**/  
    86.         /// <summary>   
    87.         /// 当前页号   
    88.         /// </summary>   
    89.         public int PageCurrent   
    90.         {   
    91.             get { return _pageCurrent; }   
    92.             set { _pageCurrent = value; }   
    93.         }   
    94.   
    95.         /// <summary>   
    96.         /// 设置页面大小   
    97.         /// </summary>   
    98.         private void GetPageCount()   
    99.         {   
    100.             if (this.NMax > 0)   
    101.             {   
    102.                 this.PageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(this.NMax) / Convert.ToDouble(this.PageSize)));   
    103.                 lblPageCount.Text = " / " + PageCount.ToString();   
    104.                 //lblPageCount1.Text = "每页 "+PageSize .ToString ()+" 条,共 "+PageCount.ToString()+" 页";   
    105.                 lblPageCount1.Text = "Page no: " + PageSize.ToString() + ",Total:" + PageCount.ToString() + " pages";   
    106.             }   
    107.             else  
    108.             {   
    109.                 this.PageCount = 0;   
    110.             }   
    111.         }   
    112.   
    113.         /**/  
    114.         /// <summary>   
    115.         /// 翻页控件数据绑定的方法 关键是这步,都是调用这里   
    116.         /// </summary>   
    117.         public void Bind()   
    118.         {   
    119.             if (this.EventPaging != null)   
    120.             {   
    121.                 this.NMax = this.EventPaging(new EventPagingArg(this.PageCurrent));   
    122.             }   
    123.   
    124.             if (this.PageCurrent > this.PageCount)   
    125.             {   
    126.                 this.PageCurrent = this.PageCount;   
    127.             }   
    128.             if (this.PageCount == 1)   
    129.             {   
    130.                 this.PageCurrent = 1;   
    131.             }   
    132.             lblcurentpage.Text = PageCurrent.ToString();   
    133.             //lblRecordCount.Text = "共有 " + NMax.ToString() + " 条记录";   
    134.             lblRecordCount.Text = "Total: " + NMax.ToString() + " records";   
    135.              
    136.   
    137.             btnPrev.Enabled = true;   
    138.             btnFirst.Enabled = true;   
    139.             btnLast.Enabled = true;   
    140.             btnNext.Enabled = true;   
    141.   
    142.             if (this.PageCurrent == 1)   
    143.             {   
    144.                 this.btnPrev.Enabled = false;   
    145.                 this.btnFirst.Enabled = false;   
    146.             }   
    147.          
    148.   
    149.             if (this.PageCurrent == this.PageCount)   
    150.             {   
    151.                 this.btnLast.Enabled = false;   
    152.                 this.btnNext.Enabled = false;   
    153.             }   
    154.         
    155.             if (this.NMax == 0)   
    156.             {   
    157.                 btnNext.Enabled = false;   
    158.                 btnLast.Enabled = false;   
    159.                 btnFirst.Enabled = false;   
    160.                 btnPrev.Enabled = false;   
    161.             }   
    162.             cmbPagecount.Items.Clear();   
    163.             for (int i = 1; i <= PageCount; i++)   
    164.                 cmbPagecount.Items.Add(i.ToString());   
    165.             cmbPagecount.SelectedIndex = PageCurrent - 1;   
    166.                
    167.         }   
    168.         /// <summary>   
    169.         /// 首页   
    170.         /// </summary>   
    171.         /// <param name="sender"></param>   
    172.         /// <param name="e"></param>   
    173.         private void btnFirst_Click(object sender, EventArgs e)   
    174.         {   
    175.             PageCurrent = 1;   
    176.             this.Bind();   
    177.         }   
    178.         //上一页   
    179.         /// <summary>   
    180.         ///    
    181.         /// </summary>   
    182.         /// <param name="sender"></param>   
    183.         /// <param name="e"></param>   
    184.         private void btnPrev_Click(object sender, EventArgs e)   
    185.         {   
    186.             PageCurrent -= 1;   
    187.             if (PageCurrent <= 0)   
    188.             {   
    189.                 PageCurrent = 1;   
    190.             }   
    191.             this.Bind();   
    192.         }   
    193.         /// <summary>   
    194.         /// 下一页   
    195.         /// </summary>   
    196.         /// <param name="sender"></param>   
    197.         /// <param name="e"></param>   
    198.         private void btnNext_Click(object sender, EventArgs e)   
    199.         {   
    200.             this.PageCurrent += 1;   
    201.             if (PageCurrent > PageCount)   
    202.             {   
    203.                 PageCurrent = PageCount;   
    204.             }   
    205.             this.Bind();   
    206.         }   
    207.         /// <summary>   
    208.         /// 最后页   
    209.         /// </summary>   
    210.         /// <param name="sender"></param>   
    211.         /// <param name="e"></param>   
    212.         private void btnLast_Click(object sender, EventArgs e)   
    213.         {   
    214.             PageCurrent = PageCount;   
    215.             this.Bind();   
    216.         }   
    217.         /// <summary>   
    218.         /// 转到新页   
    219.         /// </summary>   
    220.         /// <param name="sender"></param>   
    221.         /// <param name="e"></param>   
    222.         public void btnGo_Click(object sender, EventArgs e)   
    223.         {   
    224.             if (Int32.TryParse(cmbPagecount.SelectedItem.ToString(), out _pageCurrent))   
    225.             {   
    226.                 this.Bind();   
    227.             }    
    228.         }   
    229.     }   
    230.     /**/  
    231.     /// <summary>   
    232.     /// 自定义事件数据基类   
    233.     /// </summary>   
    234.     public class EventPagingArg : EventArgs   
    235.     {   
    236.         private int _intPageIndex;   
    237.         public EventPagingArg(int PageIndex)   
    238.         {   
    239.             _intPageIndex = PageIndex;   
    240.         }   
    241.     }   
    242. }  
    #region  版权信息
    /*---------------------------------------------------------------------*
    // 项目  名称:《Winform分页控件》
    // 文  件  名: Pager.cs
    // 描      述: 分页控件
    // 作      者:kwon yan
    *----------------------------------------------------------------------*/
    #endregion
    
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace HuishengFS.Controls
    {
        /**/
        /// <summary>
        /// 申明委托
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public delegate int EventPagingHandler(EventPagingArg e);
        /**/
        /// <summary>
        /// 分页控件呈现
        /// </summary>
        public partial class Pager : UserControl
        {
            public Pager()
            {
                InitializeComponent();
            }
            public event EventPagingHandler EventPaging;
            /**/
            /// <summary>
            /// 每页显示记录数
            /// </summary>
            private int _pageSize = 50;
            /**/
            /// <summary>
            /// 每页显示记录数
            /// </summary>
            public int PageSize
            {
                get { return _pageSize; }
                set
                {
                    _pageSize = value;
                    GetPageCount();
                }
            }
    
            private int _nMax = 0;
            /**/
            /// <summary>
            /// 总记录数
            /// </summary>
            public int NMax
            {
                get { return _nMax; }
                set
                {
                    _nMax = value;
                    GetPageCount();
                }
            }
    
            private int _pageCount = 0;
            /**/
            /// <summary>
            /// 页数=总记录数/每页显示记录数
            /// </summary>
            public int PageCount
            {
                get { return _pageCount; }
                set { _pageCount = value; }
            }
    
            private int _pageCurrent = 0;
            /**/
            /// <summary>
            /// 当前页号
            /// </summary>
            public int PageCurrent
            {
                get { return _pageCurrent; }
                set { _pageCurrent = value; }
            }
    
            /// <summary>
            /// 设置页面大小
            /// </summary>
            private void GetPageCount()
            {
                if (this.NMax > 0)
                {
                    this.PageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(this.NMax) / Convert.ToDouble(this.PageSize)));
                    lblPageCount.Text = " / " + PageCount.ToString();
                    //lblPageCount1.Text = "每页 "+PageSize .ToString ()+" 条,共 "+PageCount.ToString()+" 页";
                    lblPageCount1.Text = "Page no: " + PageSize.ToString() + ",Total:" + PageCount.ToString() + " pages";
                }
                else
                {
                    this.PageCount = 0;
                }
            }
    
            /**/
            /// <summary>
            /// 翻页控件数据绑定的方法 关键是这步,都是调用这里
            /// </summary>
            public void Bind()
            {
                if (this.EventPaging != null)
                {
                    this.NMax = this.EventPaging(new EventPagingArg(this.PageCurrent));
                }
    
                if (this.PageCurrent > this.PageCount)
                {
                    this.PageCurrent = this.PageCount;
                }
                if (this.PageCount == 1)
                {
                    this.PageCurrent = 1;
                }
                lblcurentpage.Text = PageCurrent.ToString();
                //lblRecordCount.Text = "共有 " + NMax.ToString() + " 条记录";
                lblRecordCount.Text = "Total: " + NMax.ToString() + " records";
              
    
                btnPrev.Enabled = true;
                btnFirst.Enabled = true;
                btnLast.Enabled = true;
                btnNext.Enabled = true;
    
                if (this.PageCurrent == 1)
                {
                    this.btnPrev.Enabled = false;
                    this.btnFirst.Enabled = false;
                }
          
    
                if (this.PageCurrent == this.PageCount)
                {
                    this.btnLast.Enabled = false;
                    this.btnNext.Enabled = false;
                }
         
                if (this.NMax == 0)
                {
                    btnNext.Enabled = false;
                    btnLast.Enabled = false;
                    btnFirst.Enabled = false;
                    btnPrev.Enabled = false;
                }
                cmbPagecount.Items.Clear();
                for (int i = 1; i <= PageCount; i++)
                    cmbPagecount.Items.Add(i.ToString());
                cmbPagecount.SelectedIndex = PageCurrent - 1;
                
            }
            /// <summary>
            /// 首页
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnFirst_Click(object sender, EventArgs e)
            {
                PageCurrent = 1;
                this.Bind();
            }
            //上一页
            /// <summary>
            /// 
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnPrev_Click(object sender, EventArgs e)
            {
                PageCurrent -= 1;
                if (PageCurrent <= 0)
                {
                    PageCurrent = 1;
                }
                this.Bind();
            }
            /// <summary>
            /// 下一页
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnNext_Click(object sender, EventArgs e)
            {
                this.PageCurrent += 1;
                if (PageCurrent > PageCount)
                {
                    PageCurrent = PageCount;
                }
                this.Bind();
            }
            /// <summary>
            /// 最后页
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnLast_Click(object sender, EventArgs e)
            {
                PageCurrent = PageCount;
                this.Bind();
            }
            /// <summary>
            /// 转到新页
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            public void btnGo_Click(object sender, EventArgs e)
            {
                if (Int32.TryParse(cmbPagecount.SelectedItem.ToString(), out _pageCurrent))
                {
                    this.Bind();
                } 
            }
        }
        /**/
        /// <summary>
        /// 自定义事件数据基类
        /// </summary>
        public class EventPagingArg : EventArgs
        {
            private int _intPageIndex;
            public EventPagingArg(int PageIndex)
            {
                _intPageIndex = PageIndex;
            }
        }
    }
    

    前提准备

    编写分页的SQL 语句和获取总数的SQL语句

    Sql代码 复制代码 收藏代码
    1. --分页存储过程 也可以SQL语句   
    2. --获取总数的语句 我也不必多写了   
    3. if exists(select * from sysobjects where name='Proc_DgvPage')   
    4. drop proc name='Proc_DgvPage')   
    5. go   
    6. create proc name='Proc_DgvPage')   
    7. (   
    8.  @keyword  varchar(100),    --关键字   
    9.  @start varchar(5),   
    10.  @end varchar(5)   
    11. )   
    12. as  
    13. begin  
    14.   
    15. select sortTb.clientCode, sortTb.fileReference,sortTb.clientName1, sortTb.clientName2,   
    16.     sortTb.dateOpen, sortTb.dateClose,sortTb.fileMaster,   
    17.     sortTb.clientTel, sortTb.clientMobile, sortTb.clientFex,sortTb.companyName,sortTb.clientAddr, sortTb.propFlat, sortTb.propFloor,   
    18.     sortTb.propBlock, sortTb.propBuilding, sortTb.propStree, sortTb.propDistrict, sortTb.propArea, sortTb.isFileClosed    
    19.     from (   
    20.          select  row_number() over(order by c.clientCode) as sortNo,   
    21.          c.clientCode, f.fileSubpervisor+''/''+f.fileCode+''/''+f.fileYear+''/''+f.fileIntro+''/''+f.fileHandler as fileReference,   
    22.          c.clientLastName1+'' ''+c.clientFirstName1 as clientName1, c.clientLastName2+'' ''+c.clientFirstName2 as clientName2,f.dateOpen, f.dateClose,f.fileMaster,   
    23.          c.clientTel, c.clientMobile, c.clientFex,c.companyName,c.clientAddr, f.propFlat, f.propFloor,   
    24.          f.propBlock, f.propBuilding, f.propStree, f.propDistrict, f.propArea, f.isFileClosed    
    25.          from  clientInfo c left join  clientFiles f on c.clientCode = f.fileCode    
    26.          where 1=1   
    27. )    
    28.          as sortTb    
    29.          where sortNo between @start and @end  
    30.   
    31. end  
    32. go  
    --分页存储过程 也可以SQL语句
    --获取总数的语句 我也不必多写了
    if exists(select * from sysobjects where name='Proc_DgvPage')
    drop proc name='Proc_DgvPage')
    go
    create proc name='Proc_DgvPage')
    (
     @keyword  varchar(100),	--关键字
     @start varchar(5),
     @end varchar(5)
    )
    as
    begin
    
    select sortTb.clientCode, sortTb.fileReference,sortTb.clientName1, sortTb.clientName2,
    	sortTb.dateOpen, sortTb.dateClose,sortTb.fileMaster,
    	sortTb.clientTel, sortTb.clientMobile, sortTb.clientFex,sortTb.companyName,sortTb.clientAddr, sortTb.propFlat, sortTb.propFloor,
    	sortTb.propBlock, sortTb.propBuilding, sortTb.propStree, sortTb.propDistrict, sortTb.propArea, sortTb.isFileClosed 
    	from (
    		 select  row_number() over(order by c.clientCode) as sortNo,
    		 c.clientCode, f.fileSubpervisor+''/''+f.fileCode+''/''+f.fileYear+''/''+f.fileIntro+''/''+f.fileHandler as fileReference,
             c.clientLastName1+'' ''+c.clientFirstName1 as clientName1, c.clientLastName2+'' ''+c.clientFirstName2 as clientName2,f.dateOpen, f.dateClose,f.fileMaster,
             c.clientTel, c.clientMobile, c.clientFex,c.companyName,c.clientAddr, f.propFlat, f.propFloor,
             f.propBlock, f.propBuilding, f.propStree, f.propDistrict, f.propArea, f.isFileClosed 
             from  clientInfo c left join  clientFiles f on c.clientCode = f.fileCode 
             where 1=1
    ) 
             as sortTb 
             where sortNo between @start and @end
    
    end
    go
     

    在窗体调用:

    C#代码 复制代码 收藏代码
    1. //定义变量   
    2.         DataTable dtPage;   
    3.   
    4.         /// <summary>   
    5.         /// GridViw数据绑定   
    6.         /// </summary>   
    7.         /// <returns></returns>   
    8.         private int BindDgv()   
    9.         {   
    10.         //传入要取的第一条和最后一条   
    11.             string start = (pager1.PageSize * (pager1.PageCurrent - 1) + 1).ToString();   
    12.             string end = (pager1.PageSize * pager1.PageCurrent).ToString();   
    13.   
    14.             //数据源   
    15.             dtPage = achieve.GetAll(Keyword, start, end);   
    16.             //绑定分页控件   
    17.             pager1.bindingSource1.DataSource = dtPage;   
    18.             pager1.bindingNavigator1.BindingSource = pager1.bindingSource1;   
    19.             //讲分页控件绑定DataGridView   
    20.         dgvClients.DataSource = pager1.bindingSource1;   
    21.         //返回总记录数   
    22.             return achieve.GetToalCount(Keyword);   
    23.         }   
    24.     /// <summary>   
    25.         /// 分页控件产生的事件   
    26.         /// </summary>   
    27.         private int pager1_EventPaging(HuishengFS.Controls.EventPagingArg e)   
    28.         {   
    29.             return DgvBind();   
    30.         }   
    31.   
    32.     /// <summary>   
    33.         /// 加载分页 或许写在Load事件里面   
    34.         /// </summary>   
    35.         private void FrmPage_Shown(object sender, EventArgs e)   
    36.         {  
    37.             #region DataGridView与Pager控件绑定   
    38.             this.pager1.PageCurrent = 1;//当前页为第一页   
    39.             pager1.PageSize = 100;//页数   
    40.             this.pager1.Bind();//绑定  
    41.             #endregion   
    42.         }  
    //定义变量
            DataTable dtPage;
    
            /// <summary>
            /// GridViw数据绑定
            /// </summary>
            /// <returns></returns>
            private int BindDgv()
            {
    	    //传入要取的第一条和最后一条
                string start = (pager1.PageSize * (pager1.PageCurrent - 1) + 1).ToString();
                string end = (pager1.PageSize * pager1.PageCurrent).ToString();
    
                //数据源
                dtPage = achieve.GetAll(Keyword, start, end);
                //绑定分页控件
                pager1.bindingSource1.DataSource = dtPage;
                pager1.bindingNavigator1.BindingSource = pager1.bindingSource1;
                //讲分页控件绑定DataGridView
    	    dgvClients.DataSource = pager1.bindingSource1;
     	    //返回总记录数
                return achieve.GetToalCount(Keyword);
            }
    	/// <summary>
            /// 分页控件产生的事件
            /// </summary>
            private int pager1_EventPaging(HuishengFS.Controls.EventPagingArg e)
            {
                return DgvBind();
            }
    
    	/// <summary>
            /// 加载分页 或许写在Load事件里面
            /// </summary>
            private void FrmPage_Shown(object sender, EventArgs e)
            {
                #region DataGridView与Pager控件绑定
                this.pager1.PageCurrent = 1;//当前页为第一页
                pager1.PageSize = 100;//页数
                this.pager1.Bind();//绑定
                #endregion
            }
    
  • 相关阅读:
    Genealogical tree(拓扑结构+邻接表+优先队列)
    确定比赛名次(map+邻接表 邻接表 拓扑结构 队列+邻接表)
    Agri-Net
    Network()
    Conscription
    Matrix(类似kruskal)
    Highways(求最小生成树的最大边)
    Shell学习之Shell特性(一)
    Linux学习之用户管理命令与用户组管理命令(十五)
    Linux学习之用户配置文件详解(十四)
  • 原文地址:https://www.cnblogs.com/Ruiky/p/2298194.html
Copyright © 2011-2022 走看看