使用winform的dataGridView控件实现简单的分页
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; namespace 分页 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private int pageIndex = 1; //当前的页码 private int pageSize = 7; //每页显示的记录 private int pageCount = 0; //总的页数 private int recordCount = 0; //总的记录 private void Form1_Load(object sender, EventArgs e) { loadData(); } private void loadData() { //连接字符串 string constr = "Data source=SZS\\SQLEXPRESS;INITIAL CATALOG=mysql;integrated Security=true"; using (SqlConnection conn = new SqlConnection(constr)) { string sql = "usp_fenye"; //sql语句变成存储过程名 DataTable dt = new DataTable(); using (SqlDataAdapter sqlData =new SqlDataAdapter(sql, constr)) { //表示要执行的是存储过程 sqlData.SelectCommand.CommandType = CommandType.StoredProcedure; //增加参数 SqlParameter[] pms = new SqlParameter[] { new SqlParameter("@pagesize",SqlDbType.Int){ Value=pageSize}, new SqlParameter("@index",SqlDbType.Int){ Value=pageIndex}, new SqlParameter("@recordcount",SqlDbType.Int){Direction=ParameterDirection.Output}, new SqlParameter("@pagecount",SqlDbType.Int){ Direction=ParameterDirection.Output}, }; sqlData.SelectCommand.Parameters.AddRange(pms); sqlData.Fill(dt); //获取输出的参数 label1.Text = pms[2].Value.ToString(); recordCount = Convert.ToInt32(label1.Text); label2.Text = pms[3].Value.ToString(); pageCount = Convert.ToInt32(label2.Text); label6.Text = pageIndex.ToString(); this.dataGridView1.DataSource = dt; } } } //上一页 private void button1_Click(object sender, EventArgs e) { if (pageIndex>1) { pageIndex--; loadData(); } } //下一页 private void button2_Click(object sender, EventArgs e) { if (pageIndex< pageCount) { pageIndex++; loadData(); } } } }
结果:
原文:https://www.cnblogs.com/xifengmo/p/10977178.html