C#之DataGirdView数据分页显示的实现

///数据库(mysql)连接类MySQLConnect

///若要使用请安装mysql或者将相应的代码改为Sql数据库操作代码

///“*”部分为数据库的连接的ID和密码,自行添加

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;
 
namespace测试001
{
    class MySQLConncet
    {
        private string MySqlCon = "Database=mysql;DataSource=localhost;UserID=*****;"
            + "Password=******;CharSet=utf8;port=3306";
 
        public DataTable ExecuteQuery(string sqlStr)
        {
            MySqlCommand cmd;
            MySqlConnection con;
            MySqlDataAdapter msda;
            MySqlDataReader myReader;
            DataTable InformationTable;
 
            con = new MySqlConnection(MySqlCon);
            cmd = new MySqlCommand(sqlStr,con);
            msda = new MySqlDataAdapter(cmd);
            InformationTable = new DataTable();
 
            try
            {
                con.Open();
                cmd.CommandType = CommandType.Text;
                msda.Fill(InformationTable);
                myReader = cmd.ExecuteReader();
                con.Close();
                return InformationTable;
            }
 
            catch (MySqlException ex)
            {
                Console.WriteLine(ex.Message);
                return InformationTable;
            }
            finally
            {
                con.Close();
            }
 
        }      
    }
}

///分页显示代码
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 MySql.Data.MySqlClient;
 
//数据库是Mysql
namespace测试001
{
    ///<summary>
    ///分页显示
    ///</summary>
    public partial class frmSp : Form
    {
        int pageSize = 0;    //每页显示行数
        int nMax =0;        //总记录数
        int pageCount = 0;   //页数
        int pageCurrent = 0; //当前页号
        int nCurrent = 0;    //当前记录行
        DataSet ds = new DataSet();
        DataTable dtInfor= new DataTable();
        public frmSp()
        {
            InitializeComponent();
        }
 
        private void frmSp_Load(object sender, EventArgs e)
        {
            this.splitContainer1.Dock= DockStyle.Fill;
            this.dgvInfor.Dock= DockStyle.Fill;
            this.splitContainer1.IsSplitterFixed= true;
            this.splitContainer1.FixedPanel= FixedPanel.Panel1;
 
            string str = "SELECT * FROM `t_user_info`";
            MySQLConncet mysqlCons = new MySQLConncet();
            this.dtInfor= mysqlCons.ExecuteQuery(str);
            ds = new DataSet();
            ds.Tables.Add(dtInfor);
            dtInfor = ds.Tables[0];
            InitDataSet();
        }
 
        private void InitDataSet()
        {
            pageSize = 20;//初始化每页显示行数
            nMax = dtInfor.Rows.Count;
            pageCount = (nMax / pageSize);//总页数
            if (nMax %pageSize > 0)
                ++pageCount;
            pageCurrent = 1;
            nCurrent = 0;
            LoadData();
        }
 
        private void LoadData()
        {
            int nStartPostion = 0;
            int nEndPoston = 0;
 
            DataTable dtTemp= dtInfor.Clone();//克隆DataTable结构框架
 
            if(pageCurrent == pageCount)
            {
                nEndPoston = nMax;
            }
            else
            {
                nEndPoston = pageSize *pageCurrent;//下一页的最后一个位置
            }
 
            nStartPostion = nCurrent;
 
            tsp_Label1.Text = "/" +pageCount.ToString();
            tsp_txtBox1.Text = Convert.ToString(pageCurrent);
 
            for (int i =nStartPostion; i < nEndPoston; ++i)
            {
               dtTemp.ImportRow(dtInfor.Rows[i]);
                nCurrent++;
            }
            bdsInfor.DataSource = dtTemp;
            bdnInfor.BindingSource = bdsInfor;
            dgvInfor.DataSource = bdsInfor;
        }
 
        private void bdnInfor_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if(e.ClickedItem.Text == "关闭")
                this.Close();
            if(e.ClickedItem.Text == "上一页")
            {
 
                if(pageCurrent <= 1)
                {
                    //pageCurrent++;
                    MessageBox.Show("已经是第一页", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    pageCurrent--;
                    nCurrent = pageSize *(pageCurrent - 1);
                }
                LoadData();
            }
            if(e.ClickedItem.Text == "下一页")
            {
                if(pageCurrent >= pageCount)
                {
                    MessageBox.Show("已经是最后一页", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    pageCurrent++;
                    nCurrent = pageSize *(pageCurrent - 1);
                }
                LoadData();
            }
        }
 
        private void tsp_txtBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            {
                pageCurrent = Convert.ToInt32(tsp_txtBox1.Text);
                nCurrent = pageSize *(pageCurrent - 1);
                if(pageCurrent > pageCount || pageCurrent < 1)
                {
                    MessageBox.Show("页码输入错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                LoadData();
            }
        }
    }
}

 

///界面设置,控件如下图

 

 

 

 

///运行效果

 ///demo下载链接https://download.csdn.net/download/ricardomtan/10524685

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值