C# dataGridView分页

using MySql.Data.MySqlClient;

using System;

using System.Data;

using System.Windows.Forms;

public partial class Form1 : Form

{

private const int PageSize = 100; // 每页加载的记录数量

private int currentPage = 1; // 当前页码

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

LoadData(); // 初始化加载数据

}

private void LoadData()

{

string connStr = "your_connection_string"; // MySQL连接字符串

string query = "SELECT * FROM your_table"; // 查询语句

using (MySqlConnection conn = new MySqlConnection(connStr))

{

conn.Open();

int offset = (currentPage - 1) * PageSize;

query += $" LIMIT {offset},{PageSize}"; // 添加分页限制

using (MySqlDataAdapter adapter = new MySqlDataAdapter(query, conn))

{

DataTable dataTable = new DataTable();

adapter.Fill(dataTable);

dataGridView1.DataSource = dataTable;

}

}

}

private void btnPrevious_Click(object sender, EventArgs e)

{

if (currentPage > 1)

{

currentPage--;

LoadData();

}

}

private void btnNext_Click(object sender, EventArgs e)

{

currentPage++;

LoadData();

}

}

相关推荐
leaves falling6 分钟前
C/C++ const:修饰变量和指针的区别(和引用底层关系)
c语言·开发语言·c++
比昨天多敲两行16 分钟前
C++11新特性
开发语言·c++
xiaoye-duck30 分钟前
【C++:C++11】核心特性实战:详解C++11列表初始化、右值引用与移动语义
开发语言·c++·c++11
希望永不加班44 分钟前
SpringBoot 事件机制:ApplicationEvent 与监听器
java·开发语言·spring boot·后端·spring
14年ABAP码农1 小时前
ABAP - call API with x-www-form-urlencoded
开发语言
SuniaWang1 小时前
Java 17实战:Record与密封类的黄金搭档
java·开发语言·python
2401_827499991 小时前
python项目实战10-网络机器人03
开发语言·python·php
AIminminHu1 小时前
OpenGL渲染与几何内核那点事-项目实践理论补充(三-1-(3):番外篇-当你的CAD打开“怪兽级”STL时:从内存爆炸到零拷贝的极致优化)
开发语言·c++·线程·多线程
c++逐梦人2 小时前
线程同步与互斥
linux·开发语言
坐吃山猪2 小时前
Python09_正则表达式
开发语言·python·正则表达式