• 效率分页代码


    View Code
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    //Download by http://www.codefans.net
    namespace PaginationFunction
    {
        class page
        {
            private int RowsPerPage = 5;//表示ItemsPerPage属性的默认值
            private DataSet TempSet = new DataSet();//定义一个存储数据的对象
            private int CurrentPage = 0;//表示当前处于第一页
            /// <summary>
            /// 表示每页显示多少条记录
            /// </summary>
            public int ItemsPerPage
            {
               get
               {
                  return RowsPerPage;//返回当前页显示的多少数据
               }
               set
               {
                  RowsPerPage = value;//设置当前页显示的数据数目
               }
            }
    
            public void  SetDataSet(DataSet dataSet,out DataTable dataTable)
            {
                TempSet = dataSet;//向数据集中填充内容
                GoToPageNumber(1,out dataTable);//跳转到第一页
            }
    
            /// <summary>
            /// 跳转到最后一页
            /// </summary>
            #region GoToLastPage
            public void  GoToLastPage(out DataTable pageTable)
            {
                GoToPageNumber(GetTotalPages(),out pageTable);//跳转到最后一页
            }
            #endregion
    
            /// <summary>
            /// 显示当前页的下一页记录,如果该页不存在,则不做任何操作
            /// </summary>
            #region GoToNextPage
            public void  GoToNextPage(out DataTable pageTable)
            {
                GoToPageNumber(CurrentPage + 1,out pageTable);//跳转到当前页的下一页
            }
            #endregion
    
            /// <summary>
            /// Displays the first page.
            /// </summary>
            #region GoToFirstPage
            public void  GoToFirstPage(out DataTable pageTable )
            {
                pageTable = null;//清空数据表中pageTable的记录
                DataSet TempSubSet = new DataSet();//初始化一个存储数据的数据集
                DataRow[] Rows = new DataRow[RowsPerPage];//声明一个DataRow数组
    
                if(TempSet.Tables[0].Rows.Count < RowsPerPage && CurrentPage != 1)//如果数据集中的记录总数不足一行则在第一行中显示全部
                {
                    Rows = new DataRow[TempSet.Tables[0].Rows.Count];//重新定义DataRow数组的长度
                    for(int i = 0; i < TempSet.Tables[0].Rows.Count; i++)//循环遍历数据集中的每一条记录
                    {
                        Rows[i] = TempSet.Tables[0].Rows[i];//为DataRow数组赋值
                    }
                    TempSubSet.Merge(Rows);//将当前的数据记录添加进数据集
                    pageTable = TempSubSet.Tables[0];//设定当前数据表的内容
                }
    
                if(TempSet.Tables[0].Rows.Count >= RowsPerPage && CurrentPage != 1)//当数据集中的数据记录大于每页显示记录数时
                {
                    for(int i = 0; i < RowsPerPage; i++)//循环遍历每页显示的数据数
                    {
                        Rows[i] = TempSet.Tables[0].Rows[i];//为DataRow数组赋值
                    }
                    TempSubSet.Merge(Rows);//将当前数据添加进数据集
                    pageTable = TempSubSet.Tables[0];//设定当前数据表中的内容
                }
                CurrentPage = 1;//设定当前处于第1页
              
            }
            #endregion
    
            /// <summary>
            /// 显示上一条记录
            /// </summary>
            #region GoToPreviousPage
            public void  GoToPreviousPage(out DataTable  pageTable )
            {
    
                if(CurrentPage != 1)//当当前页没有处于第1页时
                {
                    GoToPageNumber(CurrentPage - 1,out pageTable);//返回当前页的上一页
    
                }
                else//当处于第1页时
                {
                    pageTable = null;//清空数据表中的内容
                }
            }
            #endregion
    
            /// <summary>
            /// 跳转到某一页,如果该页不存在则什么也不做
            /// </summary>
            /// <param name="n">当前显示的页数</param>
            #region GoToPageNumber
            public void  GoToPageNumber(int n,out DataTable pageTable)
            {
                DataSet TempSubSet = new DataSet();//初始化一个数据集对象
                DataRow[] Rows = new DataRow[RowsPerPage];//定义一个存储数据行的数组
                int AllPages = 0;//该变量表示所有页数
                AllPages = GetTotalPages();//为AllPages变量赋值
    
                if((n > 0) && (n <= AllPages))//当变量n处于有效值范围内时
                {
                    int PageIndex = (n - 1) * RowsPerPage;//设置页索引的值
                    if(PageIndex >= TempSet.Tables[0].Rows.Count)//当页索引的值大于等于数据集中的所有记录总数时
                    {
                        GoToFirstPage(out pageTable);//返回到第一页
                    }
                    else//当页索引的值小于数据集中的所有记录总数时
                    {
                        int WholePages = TempSet.Tables[0].Rows.Count / RowsPerPage;//记录当前数据集按指定的分页方式分为多少页
                        if((TempSet.Tables[0].Rows.Count % RowsPerPage) != 0 && n == AllPages)//当变量n为总页数且有些页的数据不足时
                        {
                            Rows = new DataRow[TempSet.Tables[0].Rows.Count - (WholePages * RowsPerPage)];//表示不足一页的记录数
                        }
                        for(int i = 0,j = PageIndex; i < Rows.Length && j < TempSet.Tables[0].Rows.Count; j++,i++)//循环遍历数据集中每一条记录
                        {
                            Rows[i] = TempSet.Tables[0].Rows[j];//为数组Rows赋值
                        }
                        TempSubSet.Merge(Rows);//将不足一页的数据附加到数据集中
    
                        CurrentPage = n;//设定当前处于第几页
                        pageTable = TempSubSet.Tables[0];//为pageTable赋值
                    }
                }
                else//当变量n处于无效数据范围内时
                { 
                    pageTable = null;//清空数据表中的内容
                }
            }
            #endregion
    
            /// <summary>
            /// 该方法用来获取数据集分页后的页的总数
            /// </summary>
            /// <returns>返回当前数据集中按指定的显示方式所具有的页数 </returns>
            public int GetTotalPages()
            {
                if((TempSet.Tables[0].Rows.Count % RowsPerPage) != 0)//当数据表中的行数除以每页显示的行数的余数不为0时
                {
                    int x = TempSet.Tables[0].Rows.Count / RowsPerPage;//记录数据表中的所有行数除以每页显示的数据数
                    return (x + 1);//返回该数据集所包含的所有页数
                }
                else//当数据表中的行数刚好能正处每页显示的页数时
                {
                    return TempSet.Tables[0].Rows.Count / RowsPerPage;//返回两者相除后的值
                }
            }
        }
    }
    View Code
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;//引用与数据库操作有关的命名空间
    
    namespace PaginationFunction
    {
        public partial class PaginationFunction : Form
        {
            public PaginationFunction()
            {
                InitializeComponent();
            }
            private static string ConnectString = "server=.;database=pubs;integrated security=sspi";//定义数据库连接字符串
            private SqlConnection PaginationConnection;//定义数据库连接对象
            private SqlDataAdapter PaginationAdapter;//定义填充数据集对象
            private DataSet PaginationSet = new DataSet();//定义存储数据的集合
            private DataTable PageTable = new DataTable();//定义一个数据表
            private page Page = new page();//定义一个具有分页功能的类
    
            private void PaginationFunction_Load(object sender, EventArgs e)
            {
                DataSet PageSet = new DataSet();// 定义一个存储数据的集合
                PaginationConnection = new SqlConnection(ConnectString);//初始化数据库连接对象
                string selectString = "select emp_id as 用户编号,fname as 用户姓名,hire_date as 工作时间 from employee";//定义一个查询字符串变量
                PageSet.Clear();//清空数据集中原有内容
                FillDataTable(selectString,ref PageSet);//型数据表中填充数据
    
                Page.ItemsPerPage = 8;//设定每页显示多少行
                Page.SetDataSet(PageSet,out PageTable);//设置当前数据集中的内容
                display(PageTable);//显示数据
    
            }
    
            public bool FillDataTable(string sqlStr,ref DataSet TargetDataSet)
            {
                try
                {
                    PaginationConnection = new SqlConnection(ConnectString);//初始化数据库连接对象
                    PaginationAdapter = new SqlDataAdapter(sqlStr,PaginationConnection);//初始化数据读取对象
                    PaginationAdapter.Fill(TargetDataSet);//填充数据集
                    return true;//返回值为true
                }
                catch(Exception ex)//捕获异常
                {
                    MessageBox.Show(ex.Message);//显示异常信息
                    return false;//返回值为false
                }
                finally
                {
                    PaginationConnection.Close();//关闭数据库连接
                }
            }
    
            private void display(DataTable PageTable)
            {
                if(PageTable != null)//当数据表中存在记录时
                {
                    ListData.Rows.Clear();//清空DataGridView中原有的数据
                    object[] item = new object[PageTable.Columns.Count];//定义一个object类型的数组
                    for(int i = 0; i < PageTable.Rows.Count; i++)//循环遍历数据表中的每一行数据
                    {
                        for(int j = 0; j < PageTable.Columns.Count; j++)//循环遍历数据表中每一列数据
                        {
                            item[j] = PageTable.Rows[i][j];//保存数据表中的数据内容
                        }
                        ListData.Rows.Add(item);//向DataGridView中添加数据
                    }
                }
            }
    
            private void fistpage_Click(object sender,EventArgs e)
            {
                PageTable = null;//清空数据表中原有内容
                Page.GoToFirstPage(out PageTable);//跳转到首页
                display(PageTable);//显示数据
            }
    
            private void PreviousPage_Click(object sender,EventArgs e)
            {
                PageTable = null;//清空数据表中原有内容
                Page.GoToPreviousPage(out PageTable);//跳转到上一页
                display(PageTable);//显示数据
            }
    
            private void nextpage_Click(object sender,EventArgs e)
            {
                PageTable = null;//清空数据表中原有内容
                Page.GoToNextPage(out PageTable);//跳转到下一列
                display(PageTable);//显示数据
            }
    
            private void LastPage_Click(object sender,EventArgs e)
            {
                PageTable = null;//清空数据表中原有内容
                Page.GoToLastPage(out PageTable);//跳转到尾页
                display(PageTable);//显示数据
            }
        }
    }
  • 相关阅读:
    web自动化搞定文件上传
    App自动化08-Android SDK目录架构详解
    App自动化07-Appium日志分析,深入剖析 Appium 实现原理
    App自动化06-adb常见连接问题整理
    crontab 定时任务
    谷歌对华为断供了,那自动化测试还做吗?
    Fiddler 抓取 https 请求大全
    App自动化05-adb
    App自动化04-Inspector检查器启动QQ测试
    (转载)边缘计算与深度学习综述
  • 原文地址:https://www.cnblogs.com/anbylau2130/p/2727104.html
Copyright © 2020-2023  润新知