zoukankan      html  css  js  c++  java
  • Datagridview控件实现分页功能

    可以进行sql语句进行设置:
       
       1.先新建一个窗体,一个DataGridView控件、两个label控件、两个Button控件
       2.代码如下:

    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 _2012_4_7
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            static string str = "server=.;database=shuDB;uid=sa;pwd=accp";
            SqlConnection conn = new SqlConnection(str);
            DataSet set = new DataSet();
            SqlDataAdapter adapter;
    
            int index = 0;  //sql语句中的索引
            int yeshu = 1;  //下一页显示的页数
            int sum = 0;   //总页码
    
            //加载前6行数据
            private void Form1_Load(object sender, EventArgs e)
            {
                this.label1.Text = "" + yeshu.ToString() + "";
                string sql = @"select top 6 * from shu s";
                GetDataSet(sql);
    
                string sql2 = @"select MAX(shuid)/6 from shu";
                conn.Open();
                SqlCommand comm = new SqlCommand(sql2,conn);
                sum =(int)comm.ExecuteScalar();
                 if (sum == 0) { return; }
                 this.label2.Text = "总页数" + sum.ToString();
                 conn.Close();
            }
    
            //上一页
            private void button1_Click(object sender, EventArgs e)
            {
                if (index == 0 || yeshu == 0) { return; }  //页数是0则返回
                index--;
                yeshu--;
                this.label1.Text = "" + yeshu.ToString() + "";
                string sql = @"select top 6 * from shu where shuid not in
                    (select top (6*" + index + ") shuid from shu)";
                GetDataSet(sql);
    
            }
    
            //下一页
            private void button2_Click(object sender, EventArgs e)
            {
                if (yeshu==sum)  //如果翻的页数等于总页数
                {
                    MessageBox.Show("已经是最后一页!");
                    return;
                }        
                index++;
                yeshu++;
                this.label1.Text = "" + yeshu.ToString()+"";
                string sql = @"select top 6 * from shu where shuid not in
                    (select top (6*"+index+") shuid from shu)";
                GetDataSet(sql);
            }
    
            //绑定
            public void GetDataSet(string sql)
            {
                adapter = new SqlDataAdapter(sql, conn);
                if (set.Tables["stu"] != null)
                {
                    set.Tables["stu"].Clear();
                }
                adapter.Fill(set, "stu");
    
                this.dataGridView1.DataSource = set.Tables["stu"];
            }
        }
    }
    View Code
  • 相关阅读:
    在jsp页面如果运行时路径错误解决方法
    Maven实现ssm框架整合
    JS进阶(二)this指南——绑定了谁?
    防御性编程方法收集
    react将多个公共组件归成一类,方便调用
    初始化构建React+Ts项目时出现:Module build failed (from ./node_modules/css-loader/dist/cjs.js): CssSyntaxError
    Dva三种方式实现dispatch的Promise回调
    ES6多层解构
    ES6解构过程添加一个默认值和赋值一个新的值
    Antd-Pro2.0版本如何修改代理,让Mock变为真实服务器接口
  • 原文地址:https://www.cnblogs.com/LJSL/p/3496200.html
Copyright © 2011-2022 走看看