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 控制項綁定。
- 開啟一個新的 Visual C# .NET Windows 應用程式項目。
- 添加 DataGrid 控制項,將其 ReadOnly 屬性設定為 True。
- 將下列附加控制項放置在 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 |
|
- 複製以下代碼並將其粘貼到 Form1 代碼視窗的頂部。確保每個命名空間只被引用一次。預設情況下,可能已經引用 System 和
System.Data。using System;using System.Data;using System.Data.SqlClient;
- 複製以下代碼並將其粘貼到公用類 Form1 的頂部,以便為 Form1 聲明表單級變數:
SqlDataAdapter da;DataSet ds;DataTable dtSource;int PageCount;int maxRec;int pageSize;int currentPage;int recNo;
- 複製以下代碼並將其粘貼到緊挨在靜態空 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; }}
- 將以下代碼粘貼到 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"];
- 修改上述代碼中出現的連接字串,使之適合您的環境:
SqlConnection conn = new SqlConnection("Server=server;uid=login;pwd=pwd;database=northwind");
- 雙擊 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();
- 雙擊 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();
- 雙擊 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();
- 雙擊 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();
- 雙擊 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();
- 按 F5 鍵產生並運行此項目。
- 預設情況下,Page Size(頁面大小)設定為 5 條記錄。您可以在文字框中更改此設定。
- 單擊 Fill Grid。注意,DataGrid 中填入了 5 條記錄。
- 單擊 First Page、Next Page、Previous Page 或
Last Page 可以來回瀏覽頁面。
疑難解答
- 該方法只適用唯讀 DataGrid 控制項。當您向臨時 DataTable 對象中匯入一行時,這隻是一個副本,而您做的更改沒有儲存到主表中。
- 如果您想讓使用者能夠通過一個 DataRelation 對象定位到子記錄,或者如果您的記錄以父子關係相連結並且同時出現在表單上,則不能使用此方法(也不能用集合或數組)。