复制以下代码并将其粘贴到 Form1 代码窗口的顶部。确保每个命名空间只被引用一次。默认情况下,可能已经引用 System 和 System.Data。
using System;
using System.Data;
using System.Data.SqlClient;
5.
复制以下代码并将其粘贴到公共类 Form1 的顶部,以便为 Form1 声明窗体级变量:
SqlDataAdapter da;
DataSet ds;
DataTable dtSource;
int PageCount;
int maxRec;
int pageSize;
int currentPage;
int recNo;
6.
复制以下代码并将其粘贴到紧挨在静态的空 Main 方法之后,以使其作用范围为窗体级:
private void LoadPage() {
int i;
int startRec;
int endRec;
DataTable dtTemp;
//Clone the source table to create a temporary table.
dtTemp = dtSource.Clone();
if (currentPage == PageCount) {
endRec = maxRec;
}
else {
endRec = pageSize * currentPage;
}
startRec = recNo;
//Copy rows from the source table to fill the temporary table.
for (i = startRec; i < endRec; i++) {
dtTemp.ImportRow(dtSource.Rows[i]);
recNo += 1;
}
dataGrid1.DataSource = dtTemp;
DisplayPageInfo();
}
private void DisplayPageInfo() {
txtDisplayPageNo.Text = "Page " + currentPage.ToString() + "/ " + PageCount.ToString();
}
private bool CheckFillButton() {
// Check if the user clicks the "Fill Grid" button.
if (pageSize == 0) {
MessageBox.Show("Set the Page Size, and then click the Fill Grid button!");
return false;
}
else {
return true;
}
}
7.
将以下代码粘贴到 Form1_Load 事件过程中:
//Open Connection.
SqlConnection conn = new SqlConnection("Server=server;uid=login;pwd=pwd;database=northwind");
//Set the DataAdapter's query.
da = new SqlDataAdapter("select * from customers", conn);
ds = new DataSet();
//Fill the DataSet.
da.Fill(ds, "customers");
//Set the source table.
dtSource = ds.Tables["customers"];
8.
修改上述代码中出现的连接字符串,使之适合您的环境:
SqlConnection conn = new SqlConnection("Server=server;uid=login;pwd=pwd;database=northwind");
9.
双击 Fill Grid,打开 btnFillGrid 的代码窗口。复制以下代码并将其粘贴到 btnFillGrid_Click 事件过程中:
// Set the start and max records.
pageSize = Convert.ToInt32(txtPageSize.Text);
maxRec = dtSource.Rows.Count;
PageCount = maxRec / pageSize;
//Adjust the page number if the last page contains a partial page.
if ((maxRec % pageSize) > 0) {
PageCount += 1;
}
// Initial seeings
currentPage = 1;
recNo = 0;
// Display the content of the current page.
LoadPage();
10.
双击 First Page,打开 btnFirstPage 的代码窗口。复制以下代码并将其粘贴到 btnFirstPage_Click 事件过程中:
if (CheckFillButton() == false) {
return;
}
//Check if you are already at the first page.
if (currentPage == 1) {
MessageBox.Show("You are at the First Page!");
return;
}
currentPage = 1;
recNo = 0;
LoadPage();
11.
双击 Next Page,打开 btnNextPage 的代码窗口。复制以下代码并将其粘贴到 btnNextPage_Click 事件过程中:
//If the user did not click the "Fill Grid" button, then return.
if (CheckFillButton() == false) {
return;
}
//Check if the user clicks the "Fill Grid" button.
if (pageSize == 0) {
MessageBox.Show("Set the Page Size, and then click the Fill Grid button!");
return;
}
currentPage += 1;
if (currentPage > PageCount) {
currentPage = PageCount;
//Check if you are already at the last page.
if (recNo == maxRec) {
MessageBox.Show("You are at the Last Page!");
return;
}
}
LoadPage();
if (CheckFillButton() == false) {
return;
}
if (currentPage == PageCount) {
recNo = pageSize * (currentPage - 2);
}
currentPage -= 1;
//Check if you are already at the first page.
if (currentPage < 1) {
MessageBox.Show("You are at the First Page!");
currentPage = 1;
return;
}
else {
recNo = pageSize * (currentPage - 1);
}
LoadPage();
13.
双击 Last Page,打开 btnLastPage 的代码窗口。复制以下代码并将其粘贴到 btnLastPage_Click 事件过程中:
if (CheckFillButton() == false) {
return;
}
//Check if you are already at the last page.
if (recNo == maxRec) {
MessageBox.Show("You are at the Last Page!");
return;
}
currentPage = PageCount;
recNo = pageSize * (currentPage - 1);
LoadPage();
14.
按 F5 键生成并运行此项目。
15.
默认情况下,Page Size(页面大小)设置为 5 条记录。您可以在文本框中更改此设置。
16.
单击 Fill Grid。注意,DataGrid 中填入了 5 条记录。
17.
单击 First Page、Next Page、Previous Page 或 Last Page 可以来回浏览页面。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix