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

}

}

相关推荐
geovindu9 小时前
go: Lock/Mutex Pattern
开发语言·后端·设计模式·golang·互斥锁模式
知识分享小能手10 小时前
R语言入门学习教程,从入门到精通,R语言日期和时间序列(6)
开发语言·学习·r语言
火星papa10 小时前
C# 实现平滑流畅的进度条ProgressBar
c#·进度条·progressbar·平滑流畅
叼烟扛炮10 小时前
C++ 知识点18 内部类
开发语言·c++·算法·内部类
TAN-90°-10 小时前
Java 3——getter和setter super()关键字
java·开发语言
wand codemonkey10 小时前
(二十七)Maven(依赖)【安装】+【项目结构】
java·开发语言·maven
linda公馆10 小时前
Maven项目报错:java:错误:不支持发行版本 5
java·开发语言·maven
Ulyanov10 小时前
《从质点到位姿:基于Python与PyVista的导弹制导控制全栈仿真》: 可视化革命——基于 PyVista 的 3D 战场构建与实时渲染
开发语言·python·算法·3d·系统仿真
Heaphaestus,RC11 小时前
Slate到UMG的封装原理揭秘
开发语言·ue5
爱喝热水的呀哈喽11 小时前
一段即插即用的hypermesh命令行
开发语言·python