zoukankan      html  css  js  c++  java
  • 简单的分页用户控件(Winform)

        功能是通过一个用户控件实现的,数据库必须是sql server 2005,用到了其中的函数row_number().调用方法很简单,在每次绑定datagridview时调用InitControl()方法。窗体加载时设置行索引列名称:row_number()方法别名。[需熟悉row_number()方法]

    代码提供【上一页】,【下一页】,【首页】,【尾页】,【跳转】,当前页/总页数显示等功能,翻页时并不从数据库取数据,而是从DataTable中取数据,只是在重新查询数据时调用InitControl()方法即可更新DataTable数据.

     

     

    调用代码:

    this.ucRollPage1.RowNumberString = "rowNumber";    //设置行索引名称

    this.ucRollPage1.InitControl(10, this.dataGridView1, this.bindingSource1, dtAll);//初始化方法

     

    用户控件代码
    // InitControl初始化函数
           #region 初始化翻页控件属性,并绑定单击事件
            
    /// <summary>
            
    /// 初始化翻页控件属性,并绑定单击事件
            
    /// </summary>
            
    /// <param name="currentPage">当前页</param>
            
    /// <param name="rowsPerPage">每页记录数</param>
            public void InitControl(int rowsPerPage, DataGridView dgvList, BindingSource bindingSource1, DataTable dtAll)
            {
                
    //翻页
                this.rowsPerPage = rowsPerPage;
                this.currentRowNumber = 1;

                
    this.bindingSource1 = bindingSource1;
                
    this.dgvList = (DataGridView)dgvList;

                dtAll.AcceptChanges();
                
    this.dtAll = dtAll;

                
    this.nudCurrentPage.ValueChanged -= new EventHandler(nudCurrentPage_ValueChanged);
                
    if (this.dgvList.Rows.Count == 0)
                {
                    
    this.currentPage = 0;
                    
    this.totalPages = 0;
                    
    this.nudCurrentPage.Minimum = 0;
                    
    this.nudCurrentPage.Maximum = 0;
                }
                
    else
                {

                    
    #region 获得当前页数/总页数
                    
    this.currentPage = 1;
                    
    int ys = this.dgvList.Rows.Count % this.rowsPerPage;
                    
    if (ys == 0)
                    {
                        
    this.totalPages = this.dgvList.Rows.Count / this.rowsPerPage;
                    }
                    
    else
                    {
                        
    this.totalPages = this.dgvList.Rows.Count / this.rowsPerPage + 1;
                    }
                    
    this.lblTotalPage.Text = this.currentPage + "/" + this.totalPages;
                    
    #endregion

                    
    this.nudCurrentPage.Minimum = 1;
                    
    this.nudCurrentPage.Maximum = this.totalPages;
                }
                
    this.nudCurrentPage.ValueChanged += new EventHandler(nudCurrentPage_ValueChanged);

                GetCurrentPageData();
            }
            
    #endregion


    //获得当前页数据
        /// <summary>
            
    /// 查询当前页数据
            
    /// </summary>
            public void GetCurrentPageData()
            {
                
    //查询当前页数据
                DataTable dtTemp = this.dtAll.Copy();
                dtTemp.DefaultView.RowFilter 
    = this.rowNumberString + ">=" + this.currentRowNumber + " and " + this.rowNumberString + "<=" + this.currentPage * this.rowsPerPage;

                
    if (this.bindingSource1 == null)
                {
                    
    this.dgvList.DataSource = dtTemp.DefaultView.ToTable();

                }
                
    else
                {
                    
    this.bindingSource1.DataSource = dtTemp.DefaultView.ToTable();
                    
    this.dgvList.DataSource = this.bindingSource1;
                }

                
    //控制控件的显示
                DisplayControl();
            }

     

    完整源码请从我的群中下载: 74085440

     

  • 相关阅读:
    Rhythmk 一步一步学 JAVA(4):Spring3 MVC 之 Hello Word
    使用webclient同时post普通表单字段和文件表单字段数据到指定的URL【转】
    Rhythmk 一步一步学 JAVA(2) : 操作 MYSQL 数据库
    判断FLASH版本问题
    Rhythmk 一步一步学 JAVA(3): java JSON 数据序列化 以及反序列化
    Rhythmk 一步一步学 JAVA(5) Hibernate环境配置
    JS 数据存储
    文件下载 获取远程图片
    SQL SERVER 学习笔记
    AngularJS源码学习一 :directive
  • 原文地址:https://www.cnblogs.com/gossip/p/1616846.html
Copyright © 2011-2022 走看看