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

}

}

相关推荐
繁华似锦respect3 分钟前
C++ 设计模式之观察者模式详细介绍
linux·开发语言·c++·windows·观察者模式·设计模式·visual studio
froginwe1113 分钟前
CSS Text(文本)详解
开发语言
繁华似锦respect15 分钟前
C++ 自定义 String 类
服务器·开发语言·c++·哈希算法·visual studio
n***44317 分钟前
Java进阶:IO大全
java·开发语言·python
jtymyxmz19 分钟前
《JavaEE企业级应用开发教程(Spring+Spring MVC+Mybatis)》3.2 动态代理
java·开发语言
喵霓23 分钟前
mac系统的环境变量-bash_profile
开发语言·macos·bash
向葭奔赴♡26 分钟前
Android SharedPreferences实战指南
android·java·开发语言
CoderYanger34 分钟前
A.每日一题——2435. 矩阵中和能被 K 整除的路径
开发语言·线性代数·算法·leetcode·矩阵·深度优先·1024程序员节
我命由我1234534 分钟前
微信小程序 - 页面跳转并传递参数(使用路由参数、使用全局变量、使用本地存储、使用路由参数结合本地存储)
开发语言·前端·javascript·微信小程序·小程序·前端框架·js
️公子36 分钟前
无人直播系统-黑客主题
人工智能·c#·visual studio