//导入命名空间部分省略

DBClass.DBExecute dbexecute = new DBExecute();

string connectionString = @"Data Source=ServerName;Database=DatabaseName;integrated security=true";

#region 初始化分页显示的参数    
        static int rowsall = 0;//总行数      
        static int pageall = 0;//总页数
        static int page = 0;//第几页
        static int count = 20;//返回20行
        static int start = 0;//从第start行开始返回
        #endregion

#region 建立数据库链接
        /// <summary>
        /// 建立数据库连接
        /// </summary>
        /// <returns>返回SqlConnection对象</returns>
        public SqlConnection getcon()
        {
            SqlConnection myCon = new SqlConnection(connectionString);           
            return myCon;
        }

#region 设置DataGridView分页显示的参数,和初次绑定
        /// <summary>
        /// 设置DataGridView分页显示的参数,和初次绑定
        /// </summary>
        /// <param name="sqlstr">设置查询的sql语句</param>
        /// <param name="table">设置返回绑定的DataSet中的表的名称</param>
        /// <param name="dgv">要绑定的DataGridView</param>         
        public void upPage(string sqlstr,string table,DataGridView dgv)
        {
            rowsall = dbexecute.getds(sqlstr, table).Tables[table].Rows.Count;//总行数
            if (rowsall == 0)
            {
                //如果没有数据则将第一页、上一页、下一页、最后一页设置为不可用;并设置其他参数
                toolStripButton2.Enabled = false;
                toolStripButton3.Enabled = false;
                toolStripButton4.Enabled = false;
                toolStripButton5.Enabled = false;
                page = 0;
                pageall = 0;
                rowsall = 0;
                dgv.DataSource = null;
                tslRowsall.Text = rowsall.ToString();
                tslPageAll.Text = pageall.ToString();
                tslPage.Text = page.ToString();
                return;
            }
            if (rowsall > 0)//判断是否有内容
            {
                page = 1;//如果有内容,设置为第一页
                start = 0;
            }           
            int yushu = rowsall % count;//是否存在余行
            if (yushu == 0)//不存在余行时设置总页数
            {
                if (rowsall > 0 && rowsall <= count)
                {
                    pageall = 1;
                }
                else
                {
                    pageall = rowsall / count;
                }
            }
            else//存在余行时设置总页数
            {
                pageall = rowsall / count + 1;
            }
            {//设置显示数据,
                tslRowsall.Text = rowsall.ToString();
                tslPageAll.Text = pageall.ToString();
                tslPage.Text = page.ToString();
                if (pageall > 0)
                { //设置跳转到第几页
                    
                     tscbPage.Items.Clear();
                    for (int i = 1; i <= pageall; i++)
                        tscbPage.Items.Add(i);
                }
            }
            selectsql = sqlstr; //设置sql语句
            dgv.DataSource = gettb(selectsql ,start,count,"table")‘;//绑定DataGridView

}
        #endregion

#region
        /// <summary>
        /// 分页返回DataTable
        /// </summary>
        /// <param name="sql">查询的sql语句</param>
        /// <param name="i">从第i行开始返回</param>
        /// <param name="j">共返回j行记录</param>
        /// <param name="tablename">返回DataSet中的表明</param>
        /// <returns>返回DataTable</returns>
        public DataTable gettb(string sql, int start, int count, string tablename)
        {
            SqlConnection con = this.getcon();
            DataSet myds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(sql, con);

sda.Fill(myds, start, count, tablename);
            return myds.Tables[tablename];
        }
        #endregion

/// 第一页
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (pageall > 1)
            {
                start = 0;
                page = 1;
                tslPage.Text = page.ToString();
                this.dataGridView1.DataSource = gettb(selectsql ,start,count,"table")‘;//绑定DataGridView
            }
        }

// 上一页
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            if (page >1)
            {               
                page--;
                start -= 20;               
                tslPage.Text = page.ToString();
                 this.dataGridView1.DataSource = gettb(selectsql ,start,count,"table")‘; //绑定DataGridView
            }

}

/// 下一页
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            if (page < pageall)
            {               
                page++;
                start += 20;               
                tslPage.Text = page.ToString();
                this.dataGridView1.DataSource = gettb(selectsql ,start,count,"table")‘;//绑定DataGridView
            }      
           
        }

//最后一页
        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            if (pageall > 0)
            {
                start = (pageall - 1) * count;
                page = pageall;
                tslPage.Text = page.ToString();
                 this.dataGridView1.DataSource = gettb(selectsql ,start,count,"table")‘;//绑定DataGridView
            }
        }

上边红色部分获取总行数是调用的数据操作层里的getds方法返回一个数据集,类和方法如下:

class DBExecute
    {
        string G_str_connectionString = @"Data Source=70AB360C9ABA49E/SQLEXPRESS;Database=db_CRM;integrated security=true";//这里设置成你自己的连接
        public DBExecute(){}
        public DBExecute(string M_str_connectionString)
        {
            G_str_connectionString = M_str_connectionString;
        }
        #region 建立数据库链接
        /// <summary>
        /// 建立数据库连接
        /// </summary>
        /// <returns>返回SqlConnection对象</returns>
        public SqlConnection getcon()
        {
            string M_str_sqlcon = G_str_connectionString;
            SqlConnection myCon = new SqlConnection(M_str_sqlcon);           
            return myCon;
        }
        #endregion
#region 查询数据库返回一个DataSet对象
        /// <summary>
        /// 查询数据库返回一个DataSet对象
        /// </summary>
        /// <param name="M_str_sqlstr">SQL语句</param>
        /// <param name="M_str_table">表名</param>
        /// <returns>返回DataSet对象</returns>
        public DataSet getds(string M_str_sqlstr, string M_str_table)
        {
            DataSet myds = new DataSet();
            SqlConnection sqlcon = this.getcon();
            SqlDataAdapter sqlda = new SqlDataAdapter(M_str_sqlstr, sqlcon);
            sqlda.Fill(myds, M_str_table);          
            return myds;
        }
        #endregion
}

C# DataGridView分页显示的更多相关文章

  1. DataGridView 分页显示

    DataGridView 分页显示函数 1.获取当前页的子数据表函数 public static DataTable GetPagedTable(DataTable dt, int PageIndex ...

  2. winfrom之datagridview分页显示

    这次datagridview绑定数据并分页操作,因为用到了webservice,所以代码会详细讲解.QueryByCondition是一个查询函数 客户端: PageData pageData=new ...

  3. winform里dataGridView分页代码,access数据库

    winform里dataGridView分页,默认dataGridView是不分页的和webform里不一样,webform中GridView自带自带了分页. 现在c/s的程序很多时候也需要webfo ...

  4. C# 实现DataGridView分页功能

    C#实现DataGridView分页功能 2010-07-17 13:45:42|  分类: C#|字号 订阅     从界面可以看到,在设计时需要一个DataGridView.BindingNavi ...

  5. C#关于分页显示

    ---<PS:本人菜鸟,大手子还请高台贵手> 以下是我今天在做分页时所遇到的一个分页显示问题,使用拼写SQL的方式写的,同类型可参考哦~ ------------------------- ...

  6. 多页的TIFF图片在aspx页面分页显示

    一.逻辑实现:将数据库中的二进制TIFF图片读出并分页显示在页面上. 1.显示界面 public FrameDimension MyGuid; ; ; public static MemoryStre ...

  7. asp.net gridview 分页显示不出来的问题

    使用gridview分页显示,在点击第二页的时候显示空白,无数据. 原因是页面刷新,绑定datatable未执行 解决方法: 1.将datatable设置为静态 2.在OnPageIndexChang ...

  8. SSRS(rdl报表)分页显示表头和对表头的冻结处理

    基础环境 最近在公司做西门子某系统的二次开发,需要用到SQLServer Reporting Services(SSRS).我们用的SQL版本是SQLServer 2008 R2:在设计报表时,表格用 ...

  9. JSP分页显示实例(基于Bootstrap)

    首先介绍一款简单利落的分页显示利器:bootstrap-paginator 效果截图: GitHub官方下载地址:https://github.com/lyonlai/bootstrap-pagina ...

随机推荐

  1. javaScript this 之谜

    作为接触js没多久的人对变量作用域和this所指表示非常迷惑,专门花了一个下午google了一下,感觉以前百度到的结果都是什么鬼... 下面是我对this的认识,学疏才浅请拍砖 每一个方法都有自己的上 ...

  2. 服务追踪数据使用 RabbitMQ 进行采集 + 数据存储使用 Elasticsearch + 数据展示使用 Kibana

    服务追踪数据使用 RabbitMQ 进行采集 + 数据存储使用 Elasticsearch + 数据展示使用 Kibana https://www.cnblogs.com/xishuai/p/elk- ...

  3. 大数据Hadoop-1

    大数据Hadoop学习之搭建hadoop平台(2.2)   关于大数据,一看就懂,一懂就懵. 一.概述 本文介绍如何搭建hadoop分布式集群环境,前面文章已经介绍了如何搭建hadoop单机环境和伪分 ...

  4. Educational Codeforces Round 53 Div. 2翻车记

    A:差点开场懵逼.只要有相邻两位不同就可以作为答案. #include<iostream> #include<cstdio> #include<cmath> #in ...

  5. BZOJ4569 [SCOI2016]萌萌哒 【并查集 + 倍增】

    题目链接 BZOJ4569 题解 倍增的思想很棒 题目实际上就是每次让我们合并两个区间对应位置的数,最后的答案\(ans = 9 \times 10^{tot - 1}\),\(tot\)是联通块数, ...

  6. HttpClientUntils工具类的使用测试及注意事项(包括我改进的工具类和Controller端的注意事项【附 Json 工具类】)

    HttpClient工具类(我改过): package com.taotao.httpclient; import java.io.IOException; import java.net.URI; ...

  7. mysql的对象

      mysql 常见的数据对象有哪些: DataBase/Schema Table Index View/Trigger/Function/Procedure   多Database用途: 业务的隔离 ...

  8. 取消eslint对指定代码进行代码检测

    eslint配置了不允许使用alert,但是有个需求需要用到. //eslint-disable-next-line alert('测试'); 如上,即可跳过当前行代码检查了

  9. 入门级:GitHub和Git超超超详细使用教程!

    GitHub和Git入门 考虑到大家以前可能对版本控制工具和Linux命令行工具都不了解,我写了一个简单的博客来让大家学会入门使用方法. GitHub的简单使用 第一步 创建GitHub账号 1. 打 ...

  10. React 获取 url 参数 —— this.props.match

    在 react 组件的  componentDidMount 方法中打印一下 this.props,在浏览器控制台中查看输出如下: 其中页面的 url 信息全都包含在 match 字段中,以地址 lo ...