???? ID: 305271 - ????? ???????: 04 ?????? 2010 - ??????: 2.0

How To Perform Paging with the DataGrid Windows Control by Using Visual Basic .NET

?????? ??????This article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.

?? ????? ??

??? ?? ??????? ???? | ??? ?? ??????? ????

??????

TheDataGridWeb control has built-in Automatic or Custom Paging functionalities; however, theDataGridWindows control lacks these features. This article demonstrates how to build a simple paging mechanism for theDataGridWindows control.

The code samples in this article make use ofDataSet???????? ??? In ADO.NET,DataSet???????? ???? ??? ???????? ??? ??? ?? ?????? ??? ????? ??? ???? ??? ?? ???? ??? ?? ??? ??? ?? ??? ???DataSet, ?? ???? ????? ?? ?? ???? ??? ??? ???? ????????? ???? ?? ??? ?? ????????? ?????? ??????

?? ????? ?? ??? ?????? ??? ??? ????? ??????????? ?????????? ??????? ?? ??? ????

??????????

  • Microsoft Visual Basic .NET
  • Microsoft SQL Server ????????? ????? ???????

??? DataGrid Windows ?? ???????? ???? ?? ??? ?????? ?????? ???? ?? ??? ???

?? ?? ????? ??DataGrid, ?????-???? ??? "???," ??, ???? ?? ??? ?? ??? ??? ???????? ?? ???? ?? ????????? ???? ????????? ?? ???? ???? ?? ??? ????? ???DataRow???????? ????? ?? ?? ??? ????????DataSet??? ??? ??????? ?????? ?? ??? ??????? ??????? ?????? ??? ???? ?? ??? ????? ??DataGrid???????? ???
  1. ??? Visual Basic .NET Windows ????????? ?? ?????? Form1 ???????? ??? ?? ???? ??..
  2. addDataGrid??? ????, ?? ???????? ????ReadOnly???? ?? ??? ???True.
  3. ????? ???????? ???????? Form1 ?? ????, ?? ???? ????? ?? ?? ??? ??? ???? ??? ??? ????:
    ?? ?????? ?? ??????? ?????? ?????? ?? ??????? ????
    ??????????? ?????? ?? ???
    ???btnFirstPage????? ?????
    ???btnNextPage???? ?????
    ??? ?????txtDisplayPageNo
    ???btnPreviousPage????? ?????
    ???btnLastPage????? ?????
    ??? ?????txtPageSize5
    ???btnFillGrid????? ????

  4. ?? ????????? ?????, ?? Form1 ?? ??????? Declaration ?????? ??? ????? ??? ???????:
    Imports System
    Imports System.Data
    Imports System.Data.SqlClient
    					
  5. ?? ????????? ????? ?? ????? ??? ???????????"Windows ??????? ??????? ??? ????? ???? ???" ??????? 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. ????? ????: ????? ??? ?? ??? ???????? ????Form1 ??????
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
      Handles MyBase.Load
    
    End Sub
    					
  7. ?? ????????? ????? ?? ????? ??? ??????????? ???"Windows ??????? ??????? ??? ????? ???? ???" ??????? ???:
    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???????? ??? ????? ?? it ????????? ??????? ??? ?? ?????? ??????? ?? ????? ???? ???
  9. ????? ?? ????????? ?? ????? ?? ??? F5 ????? ??????
  10. ???????? ??? ??, ????? ???? ??? ??, 5 ??????? ???? ?? ??? ???? ?? ??? ????? ??? ??? ??? ???? ????
  11. ????? ????,????? ????. ????? ??? ?? ????? 5 ??????? ?? ??? ???
  12. ????? ????,????? ?????,???? ?????,????? ?????, ??????? ???????????? ?? ??? ??????? ???? ???? ?? ????

?????? ??????

  • ???? ?? ??? ?? ????? ???? ??? ???? ???? -DataGrid???????? ??? ?? ?? ???? ?????? ?? ???? ?? ???????DataTable????????, ?? ?? ????????? ?????? ??????, ?? ?? ???????? ????? ???? ?????? ????? ?????? ?? ????
  • ?? ????? ?? ??? ???? ???? ?? (?? neither ?????? ?? ???? ??) ??? ?? ?????????? ?????? ?????? ?????? ??? ??????? ???? ??? ????? ???? ?? ??? ???DataRelation???????? ?? ??? ???? ??? ?????? ??? ?? ?????? ?????? relation ?? ???? ?? ?? ??? ?? ??????? ?? ?? ?? ??? ??? ??? ???? ????

??????

ADO.NET ?? ???? ??? ???? ??????? ?? ??? ????? ????? MSDN ??? ????:
ADO.NET ?? ??? ???? ?? ????? ??? ??
(vs.71) http://msdn2.Microsoft.com/en-us/library/e80y5yhx .aspx (http://msdn2.microsoft.com/en-us/library/e80y5yhx(vs.71).aspx)
???? ??????? ?? ???, Microsoft .NET Framework SDK ????? ????????:
.NET ????? SDK
(VS.71) http://msdn2.Microsoft.com/en-us/library/aa719465 .aspx (http://msdn2.microsoft.com/en-us/library/aa719465(VS.71).aspx)

???? ???? ???? ??:
  • Microsoft ADO.NET 1.1
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
??????: 
kbdatabinding kbhowtomaster kbwindowsforms kbmt KB305271 KbMthi
???? ?????? ???????????? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:305271  (http://support.microsoft.com/kb/305271/en-us/ )