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

}

}

相关推荐
Tanjia_kiki4 小时前
完美解决.NET Framework 4.0 中 System.Drawing 库不支持 WebP 格式的图像处理
c#·.net
景天科技苑5 小时前
【Rust通用集合类型】Rust向量Vector、String、HashMap原理解析与应用实战
开发语言·后端·rust·vector·hashmap·string·rust通用集合类型
阿沁QWQ6 小时前
友元函数和友元类
开发语言·c++
小黑随笔7 小时前
【Golang玩转本地大模型实战(一):ollma部署模型及流式调用】
开发语言·后端·golang
江沉晚呤时7 小时前
Redis缓存穿透、缓存击穿与缓存雪崩:如何在.NET Core中解决
java·开发语言·后端·算法·spring·排序算法
achene_ql7 小时前
缓存置换:用c++实现最近最少使用(LRU)算法
开发语言·c++·算法·缓存
高效匠人8 小时前
Python10天冲刺-设计模型之策略模式
开发语言·人工智能·python·策略模式
黄雪超8 小时前
JVM——JVM 是如何执行方法调用的?
java·开发语言·jvm
风暴之零8 小时前
文本中地理位置提取方法—正则和NLP模型
开发语言·python
Dxy12393102169 小时前
python合并word中的run
开发语言·python·word