Winform 实现分页效果

效果图

实现如下图,可以设置显示行数,当前页数,总记录,各个页数跳转。
在这里插入图片描述

数据库准备

SqlServer2008 R2, 建立一个数据表名为:winformBiao,
字段为ID,NAME,DES.

在这里插入图片描述

winform界面搭建

1:在工具栏拖拽一个dataGridView1控件到窗口,查看属性,
2:colmuns集合点进去,设置列名,
3:添加显示列
4:设置名称属性,
5:DataPropertyName 和数据库表的列名一一对应。
在这里插入图片描述下边分页控件自己简单搭建,做的很粗糙,见谅,重要的是思路方法。

在这里插入图片描述Name:
1:EveryPageCounts,为其添加方法txt_lines_Leave,只要内容改变,就触发
2: lb_page
3: lb_recordNum
4: 设置4个按钮的page值:0,1,2,3 ,方便代码统一复用。
5:txt_page
6:添加跳转方法

在这里插入图片描述在这里插入图片描述

功能实现

1:工具类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace WindowsFormsApplicationTest
{
    class ConnectClass
    {

        public static string host = "MSI\\SQLEXPRESS"; //192.168.0.101    WIN-8P7DPTR7QTV\\SQLEXPRESS
        public static string id = "sa";    //  sa
        public static string pwd = "123";  //123    
        public static string database = "LBDatabase2";  //lbDatabase
        public static string result = "";
        public static string connectionString = string.Format("Server = {0};Database = {1};User ID = {2};Password = {3};MultipleActiveResultSets = {4};", host, database, id, pwd, true);

        private static SqlConnection conn = new SqlConnection(connectionString);

        public static DataSet ReturnDataSet(string sql)
        {
            conn.Open();
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
            sda.Fill(ds,"objDataSet");
            conn.Close();
            return ds;       
        }

      // 如下图:每页: 2 行,  当前页:2 /4     共:7条记录
      //选择 前2行,条件: 排除前(2-1)* 2 = 2行的表中,也就是第二页显示的数据:表第3.4行。
     
      // ReturnSelectSql(2,0)  第一页    得到第  1,2  条数据
      // ReturnSelectSql(2,2)  第二页    得到第  3,4  条数据
      // ReturnSelectSql(2,6)  第二页    得到第  5,6  条数据
        public static string ReturnSelectSql(int count,int totalCount)
        {
            string sql = "select top " + count + " * from winformBiao where id not in (select top " + totalCount + " id from winformBiao) ";
            return sql;
        }


    }
}

在这里插入图片描述
主窗口form

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;

namespace WindowsFormsApplicationTest
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private int currentPageCount;//记录当前页行数:每页行数

        private int pageCount;//记录总页数  共多少页

        private int currentCount=1;//记录当前页数:第几页

       //界面加载时,初始化下边分页的数据,和表格数据
        private void Form3_Load(object sender, EventArgs e)
        {
            currentPageCount = Convert.ToInt32(EveryPageCounts.Text);  //50
            string sql = "select * from winformBiao";
            DataSet ds = ConnectClass.ReturnDataSet(sql);
            Paging(ds);
           
            this.currentCount = 1;
            Binds();
        }

        private void Paging(DataSet ds)
        {
            int pageLine = 0;
            int count = ds.Tables[0].Rows.Count; //总行数
            lb_recordNum.Text = count.ToString();  //共?条记录 7
            pageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(count) / currentPageCount)); //3
            if (count > currentPageCount)
                pageLine = currentPageCount;
            else
                pageLine = count;
            this.currentPageCount = pageLine;
        }
        
        //4个按钮的click共同方法
         private void btn_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            string tag = btn.Tag.ToString();//获取首页、上一页、下一页、末页的tag属性值
            switch (tag)
            {
                case "0"://首页
                    this.currentCount = 1;
                    break;
                case "1"://上一页
                    this.currentCount -= 1;
                    break;
                case "2"://下一页
                    this.currentCount += 1;
                    break;
                case "3"://末页
                    this.currentCount = this.pageCount;
                    break;
            }
            Binds();
        }

       //显示表格筛选的数据
        private void Binds()
        {
            string sql = string.Empty;
            if (this.currentCount.Equals(0))
            {
                currentCount = 1;
                MessageBox.Show("当前为首页");
            }
            else if (this.currentCount.Equals(pageCount + 1))
            {
                this.currentCount = this.pageCount;
                MessageBox.Show("当前为末页");
            }
            else if (this.currentCount > 0 && this.currentCount < pageCount + 1)
            {
                sql = ConnectClass.ReturnSelectSql(currentPageCount, (currentCount - 1) * currentPageCount);
                DataSet ds = ConnectClass.ReturnDataSet(sql);
                this.dataGridView1.DataSource = ds.Tables[0];
                lb_page.Text = this.currentCount + " / " + pageCount;
            }
        }

        //跳转指定页面
        private void button5_Click(object sender, EventArgs e)
        {
             if (txt_page.Text == "")
                return;
            this.currentCount = Convert.ToInt32(txt_page.Text);
            if (this.currentCount > 0 && this.currentCount < pageCount + 1)
                Binds();

        }

        //设置每行页数
        private void txt_lines_Leave(object sender, EventArgs e)
        {
            Form3_Load(sender, e);
        }
 


    }
}

  • 7
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值