DataGrid Web 控制項都有內建的自動] 或 [自訂分頁功能 ; 不過,
DataGrid Windows 控制項缺少這些功能。本文將告訴您,如何建置一個簡單的分頁機制,
DataGrid Windows 控制項。
本文的程式碼範例讓使用
資料集 的物件。
資料集 物件會在單一作業中填滿和常駐在記憶體 ADO.NET 中, 所有的時間。如果您正在使用大型
資料集,本文將告訴您,如何以程式設計的方式顯示資料區塊 (Chunk) 或網頁中。
這項技術有一些限制。請參閱
Troubleshooting 一節,如需詳細資訊。
需求
- Microsoft Visual Basic.NET
- Microsoft SQL Server 北風貿易範例資料庫
步驟 DataGrid Windows 控制項中加入分頁
當您逐頁
DataGrid 時,您在 [頁面大小 「 區塊 」 也就是一頁的記錄一次顯示資料。範例程式碼,請依照下列會將每一頁的
DataRow 物件從記憶體中的
資料集 複製到暫存資料表。暫存資料表再繫結至
DataGrid 控制項。
- 開啟新的 Visual Basic.NET Windows 應用程式。預設會建立 Form1。
- 加入 DataGrid 控制項,並將其 唯讀] 屬性設定為 True。
- 放入以下的其他控制項上 Form1,並設定其屬性,如下所示:
摺疊此表格展開此表格
| 控制項 | 名稱屬性 | 文字屬性 |
|---|
| 按鈕 | btnFirstPage | 第一頁 |
| 按鈕 | btnNextPage | 下一個頁面 |
| 文字方塊 | txtDisplayPageNo | |
| 按鈕 | btnPreviousPage | 前一頁 |
| 按鈕 | btnLastPage | 最後一頁 |
| 文字方塊 | txtPageSize | 5 |
| 按鈕 | btnFillGrid | 填滿方格 |
- 複製,並將下列程式碼貼到 Form1 的一般宣告區段:
Imports System
Imports System.Data
Imports System.Data.SqlClient
- 複製並貼上在下列程式碼 之前 「 Windows Form 設計工具產生的程式碼 」] 區域,以將表單層級變數宣告為 Form1:
Private da As SqlDataAdapter
Private ds As DataSet
Private dtSource As DataTable
Private PageCount As Integer
Private maxRec As Integer
Private pageSize As Integer
Private currentPage As Integer
Private recNo As Integer
- 刪除下列自動產生的程式碼的 Load 事件中的 Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
End Sub
- 複製並貼上在下列程式碼 之後"Windows Form 設計工具產生的程式碼] 區域:
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'Open Connection.
Dim conn As SqlConnection = New SqlConnection( _
"Server=(local)\netsdk;uid=sa;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")
End Sub
Private Sub btnNextPage_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnNextPage.Click
'If the user did not click the "Fill Grid" button then Return
If Not CheckFillButton() Then Return
'Check if the user clicked the "Fill Grid" button.
If pageSize = 0 Then
MessageBox.Show("Set the Page Size, and then click the ""Fill Grid"" button!")
Return
End If
currentPage = currentPage + 1
If currentPage > PageCount Then
currentPage = PageCount
'Check if you are already at the last page.
If recNo = maxRec Then
MessageBox.Show("You are at the Last Page!")
Return
End If
End If
LoadPage()
End Sub
Private Sub btnPreviousPage_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnPreviousPage.Click
If Not CheckFillButton() Then Return
If currentPage = PageCount Then
recNo = pageSize * (currentPage - 2)
End If
currentPage = currentPage - 1
'Check if you are already at the first page.
If currentPage < 1 Then
MessageBox.Show("You are at the First Page!")
currentPage = 1
Return
Else
recNo = pageSize * (currentPage - 1)
End If
LoadPage()
End Sub
Private Sub LoadPage()
Dim i As Integer
Dim startRec As Integer
Dim endRec As Integer
Dim dtTemp As DataTable
Dim dr As DataRow
'Duplicate or clone the source table to create the temporary table.
dtTemp = dtSource.Clone
If currentPage = PageCount Then
endRec = maxRec
Else
endRec = pageSize * currentPage
End If
startRec = recNo
'Copy the rows from the source table to fill the temporary table.
For i = startRec To endRec - 1
dtTemp.ImportRow(dtSource.Rows(i))
recNo = recNo + 1
Next
DataGrid1.DataSource = dtTemp
DisplayPageInfo()
End Sub
Private Sub btnFirstPage_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnFirstPage.Click
If Not CheckFillButton() Then Return
' Check if you are already at the first page.
If currentPage = 1 Then
MessageBox.Show("You are at the First Page!")
Return
End If
currentPage = 1
recNo = 0
LoadPage()
End Sub
Private Sub btnLastPage_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnLastPage.Click
If Not CheckFillButton() Then Return
' Check if you are already at the last page.
If recNo = maxRec Then
MessageBox.Show("You are at the Last Page!")
Return
End If
currentPage = PageCount
recNo = pageSize * (currentPage - 1)
LoadPage()
End Sub
Private Sub DisplayPageInfo()
txtDisplayPageNo.Text = "Page " & currentPage.ToString & "/ " & PageCount.ToString
End Sub
Private Sub btnFillGrid_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnFillGrid.Click
'Set the start and max records.
pageSize = txtPageSize.Text
maxRec = dtSource.Rows.Count
PageCount = maxRec \ pageSize
' Adjust the page number if the last page contains a partial page.
If (maxRec Mod pageSize) > 0 Then
PageCount = PageCount + 1
End If
'Initial seeings
currentPage = 1
recNo = 0
' Display the content of the current page.
LoadPage()
End Sub
Private Function CheckFillButton() As Boolean
'Check if the user clicks the "Fill Grid" button.
If pageSize = 0 Then
MessageBox.Show("Set the Page Size, and then click the ""Fill Grid"" button!")
CheckFillButton = False
Else
CheckFillButton = True
End If
End Function
- 修改 ConnectionString 參數程式碼中的,讓它指向 Northwind 資料庫的現有執行個體。
- 按下 F5 鍵以建置並執行專案。
- 預設情況下,頁面大小設定為 5 的記錄,讓您可以變更它在文字方塊中。
- 按一下 [填滿方格。請注意 [格線 5 記錄以填滿。
- 按一下 [第一頁、 下一頁、 上一頁 及頁面之間瀏覽 最後一頁。
疑難排解
- 這項技巧只適用於唯讀 DataGrid 控制項中。暫時的 DataTable 物件中匯資料列時您會建立一個複本。因此,您所做的變更將不會儲存到主資料表中。
- 這項技術仍無法解決問題 (以及兩者都不會執行集合或陣列) 如果您想要使用者能夠瀏覽到透過 DataRelation 物件的子資料錄或有記錄在父子式關聯連結同時出現在表單上。
如需關於 ADO.NET,請參閱下列 MSDN 網站:
如需詳細資訊,請參閱 Microsoft.NET Framework SDK 文件: