文章編號: 305271 - 上次校閱: 2007年5月13日 - 版次: 4.2

如何使用 Visual Basic.NET 執行與 DataGrid Windows 控制項分頁

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。

在此頁中

全部展開 | 全部摺疊

結論

DataGrid Web 控制項都有內建的自動] 或 [自訂分頁功能 ; 不過,DataGrid Windows 控制項缺少這些功能。本文將告訴您,如何建置一個簡單的分頁機制,DataGrid Windows 控制項。

本文的程式碼範例讓使用 資料集 的物件。資料集 物件會在單一作業中填滿和常駐在記憶體 ADO.NET 中, 所有的時間。如果您正在使用大型 資料集,本文將告訴您,如何以程式設計的方式顯示資料區塊 (Chunk) 或網頁中。

這項技術有一些限制。請參閱 Troubleshooting 一節,如需詳細資訊。

需求

  • Microsoft Visual Basic.NET
  • Microsoft SQL Server 北風貿易範例資料庫

步驟 DataGrid Windows 控制項中加入分頁

當您逐頁 DataGrid 時,您在 [頁面大小 「 區塊 」 也就是一頁的記錄一次顯示資料。範例程式碼,請依照下列會將每一頁的 DataRow 物件從記憶體中的 資料集 複製到暫存資料表。暫存資料表再繫結至 DataGrid 控制項。
  1. 開啟新的 Visual Basic.NET Windows 應用程式。預設會建立 Form1。
  2. 加入 DataGrid 控制項,並將其 唯讀] 屬性設定為 True
  3. 放入以下的其他控制項上 Form1,並設定其屬性,如下所示:
    摺疊此表格展開此表格
    控制項名稱屬性文字屬性
    按鈕btnFirstPage第一頁
    按鈕btnNextPage下一個頁面
    文字方塊txtDisplayPageNo
    按鈕btnPreviousPage前一頁
    按鈕btnLastPage最後一頁
    文字方塊txtPageSize5
    按鈕btnFillGrid填滿方格

  4. 複製,並將下列程式碼貼到 Form1 的一般宣告區段:
    Imports System
    Imports System.Data
    Imports System.Data.SqlClient
    					
  5. 複製並貼上在下列程式碼 之前 「 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
    					
  6. 刪除下列自動產生的程式碼的 Load 事件中的 Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
      Handles MyBase.Load
    
    End Sub
    					
  7. 複製並貼上在下列程式碼 之後"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
    					
  8. 修改 ConnectionString 參數程式碼中的,讓它指向 Northwind 資料庫的現有執行個體。
  9. 按下 F5 鍵以建置並執行專案。
  10. 預設情況下,頁面大小設定為 5 的記錄,讓您可以變更它在文字方塊中。
  11. 按一下 [填滿方格。請注意 [格線 5 記錄以填滿。
  12. 按一下 [第一頁下一頁上一頁 及頁面之間瀏覽 最後一頁

疑難排解

  • 這項技巧只適用於唯讀 DataGrid 控制項中。暫時的 DataTable 物件中匯資料列時您會建立一個複本。因此,您所做的變更將不會儲存到主資料表中。
  • 這項技術仍無法解決問題 (以及兩者都不會執行集合或陣列) 如果您想要使用者能夠瀏覽到透過 DataRelation 物件的子資料錄或有記錄在父子式關聯連結同時出現在表單上。

?考

如需關於 ADO.NET,請參閱下列 MSDN 網站:
存取使用 ADO.NET 的資料
http://msdn2.microsoft.com/en-us/library/e80y5yhx(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/e80y5yhx(vs.71).aspx)
如需詳細資訊,請參閱 Microsoft.NET Framework SDK 文件:
.NET Framework SDK
http://msdn2.microsoft.com/en-us/library/aa719465(VS.71).aspx (http://msdn2.microsoft.com/en-us/library/aa719465(VS.71).aspx)

這篇文章中的資訊適用於:
  • Microsoft ADO.NET 1.0
  • Microsoft ADO.NET 1.1
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
關鍵字:?
kbmt kbdatabinding kbhowtomaster kbwindowsforms KB305271 KbMtzh
機器翻譯機器翻譯
重要:本文是以 Microsoft 機器翻譯軟體翻譯而成,而非使用人工翻譯而成。Microsoft 同時提供使用者人工翻譯及機器翻譯兩個版本的文章,讓使用者可以依其使用語言使用知識庫中的所有文章。但是,機器翻譯的文章可能不盡完美。這些文章中也可能出現拼字、語意或文法上的錯誤,就像外國人在使用本國語言時可能發生的錯誤。Microsoft 不為內容的翻譯錯誤或客戶對該內容的使用所產生的任何錯誤或損害負責。Microsoft也同時將不斷地就機器翻譯軟體進行更新。
按一下這裡查看此文章的英文版本:305271? (http://support.microsoft.com/kb/305271/en-us/ )
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。