天天看點

如何使用 Visual C# .NET 對 DataGrid Windows 控件執行分頁如何使用 Visual C# .NET 對 DataGrid Windows 控件執行分頁

如何使用 Visual C# .NET 對 DataGrid Windows 控件執行分頁

察看本文應用于的産品

文章編号 : 307710
最後修改 : 2005年3月23日
修訂 : 3.1

本文的釋出号曾為 CHS307710

本頁
概要
要求
向 DataGrid Windows 控件中添加分頁的步驟
疑難解答
參考
這篇文章中的資訊适用于:

概要

DataGrid Web 控件有内置的自動或自定義分頁功能,而 DataGrid Windows 控件則沒有。本文示範了如何為 DataGrid Windows 控件建立簡單的分頁機制。

本文的代碼示例利用了 DataSet 對象。在 ADO.NET 中,DataSet 對象是通過單次操作填充的并且永駐在記憶體中。如果您正在使用一個大型 DataSet,本文将為您介紹如何通過程式設計按塊或頁顯示資料。

本示例以 Microsoft SQL Server 羅斯文資料庫中的“客戶”表為資料庫後端。如果您連接配接的是其他資料庫或表,請確定相應更新代碼。

此方法有一定局限性。請參考疑難解答 一節以了解詳細資訊。

回到頂端

要求

下表列出了推薦使用的硬體、軟體、網絡架構以及所需的 Service Pack:

Microsoft Windows 2000 Professional、Windows 2000 Server、Windows 2000 Advanced Server 或 Windows NT 4.0 Server
Microsoft Visual Studio .NET
Microsoft SQL Server 7.0 或更高版本

本文假定您熟悉下列主題:

Visual C# .NET
ADO.NET 基礎知識和文法
回到頂端

向 DataGrid Windows 控件中添加分頁的步驟

當您對 DataGrid 分頁時,資料會在頁大小的“塊”中顯示,即一次顯示一頁記錄。要效仿的這個示例代碼将每頁的 DataRow 對象從記憶體中的 DataSet 複制到一個臨時表中。該臨時表随後與 DataGrid 控件綁定。

1. 打開一個新的 Visual C# .NET Windows 應用程式項目。
2. 添加 DataGrid 控件,将其 ReadOnly 屬性設定為 True。
3.

将下列附加控件放置在 Form1 上,并按如下所示設定它們的屬性:

控件

Name 屬性

Text 屬性

Button

btnFirstPage

First Page

Button

btnNextPage

Next Page

TextBox

txtDisplayPageNo

Button

btnPreviousPage

Previous Page

Button

btnLastPage

Last Page

TextBox

txtPageSize

5

Button

btnFillGrid

Fill Grid

DataGrid

dataGrid1

控件 Name 屬性 Text 屬性 Button btnFirstPage First Page Button btnNextPage Next Page TextBox txtDisplayPageNo Button btnPreviousPage Previous Page Button btnLastPage Last Page TextBox txtPageSize 5 Button btnFillGrid Fill Grid DataGrid dataGrid1
控件 Name 屬性 Text 屬性
Button btnFirstPage First Page
Button btnNextPage Next Page
TextBox txtDisplayPageNo
Button btnPreviousPage Previous Page
Button btnLastPage Last Page
TextBox txtPageSize 5
Button btnFillGrid Fill Grid
DataGrid dataGrid1
4.

複制以下代碼并将其粘貼到 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();

12.

輕按兩下 Previous Page,打開 btnPreviousPage 的代碼視窗。複制以下代碼并将其粘貼到 btnPreviousPage_Click 事件過程中: 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 可以來回浏覽頁面。
回到頂端

疑難解答

該方法隻适用隻讀 DataGrid 控件。當您向臨時 DataTable 對象中導入一行時,這隻是一個副本,而您做的更改沒有儲存到主表中。
如果您想讓使用者能夠通過一個 DataRelation 對象定位到子記錄,或者如果您的記錄以父子關系相連結并且同時出現在窗體上,則不能使用此方法(也不能用集合或數組)。
回到頂端