向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 |
|
|
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 對象定位到子記錄,或者如果您的記錄以父子關係相連結並且同時出現在表單上,則不能使用此方法(也不能用集合或數組)。 |