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

{

    classMySQLConncet

    {

        privatestringMySqlCon = "Database=mysql;DataSource=localhost;UserID=*****;"

            + "Password=******;CharSet=utf8;port=3306";

 

        publicDataTableExecuteQuery(string sqlStr)

        {

            MySqlCommand cmd;

            MySqlConnection con;

            MySqlDataAdapter msda;

            MySqlDataReader myReader;

            DataTableInformationTable;

 

            con = newMySqlConnection(MySqlCon);

            cmd = newMySqlCommand(sqlStr,con);

            msda = newMySqlDataAdapter(cmd);

            InformationTable = newDataTable();

 

            try

            {

                con.Open();

                cmd.CommandType = CommandType.Text;

                msda.Fill(InformationTable);

                myReader = cmd.ExecuteReader();

                con.Close();

                returnInformationTable;

            }

 

            catch (MySqlException ex)

            {

                Console.WriteLine(ex.Message);

                returnInformationTable;

            }

            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>

    publicpartialclassfrmSp : Form

    {

        intpageSize = 0;    //每页显示行数

        int nMax =0;        //总记录数

        intpageCount = 0;   //页数

        intpageCurrent = 0; //当前页号

        intnCurrent = 0;    //当前记录行

        DataSet ds = newDataSet();

        DataTable dtInfor= newDataTable();

        public frmSp()

        {

            InitializeComponent();

        }

 

        privatevoidfrmSp_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 = newMySQLConncet();

            this.dtInfor= mysqlCons.ExecuteQuery(str);

            ds = newDataSet();

            ds.Tables.Add(dtInfor);

            dtInfor = ds.Tables[0];

            InitDataSet();

        }

 

        privatevoidInitDataSet()

        {

            pageSize = 20;//初始化每页显示行数

            nMax = dtInfor.Rows.Count;

            pageCount = (nMax / pageSize);//总页数

            if (nMax %pageSize > 0)

                ++pageCount;

            pageCurrent = 1;

            nCurrent = 0;

            LoadData();

        }

 

        privatevoidLoadData()

        {

            intnStartPostion = 0;

            intnEndPoston = 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;

        }

 

        privatevoidbdnInfor_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();

            }

        }

 

        privatevoidtsp_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



猜你喜欢

转载自blog.csdn.net/ricardomtan/article/details/80941302