Kamis, 16 Desember 2010

How to Paging in DataGridView

datagrid-paging
Imports System.Data.SqlClient
Public Class Form1
    Dim pagingAdapter As SqlDataAdapter
    Dim pagingDS As DataSet
    Dim scrollVal As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim connectionString As String = "Data Source=.;Initial Catalog=pubs;Integrated Security=True"
        Dim sql As String = "SELECT * FROM authors"
        Dim connection As New SqlConnection(connectionString)
        pagingAdapter = New SqlDataAdapter(sql, connection)
        pagingDS = New DataSet()
        connection.Open()
        pagingAdapter.Fill(pagingDS, scrollVal, 5, "authors_table")
        connection.Close()
        DataGridView1.DataSource = pagingDS
        DataGridView1.DataMember = "authors_table"
    End Sub

    Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
        scrollVal = scrollVal - 5
        If scrollVal <= 0 Then
            scrollVal = 0
        End If
        pagingDS.Clear()
        pagingAdapter.Fill(pagingDS, scrollVal, 5, "authors_table")
    End Sub

    Private Sub button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button3.Click
        scrollVal = scrollVal + 5
        If scrollVal > 23 Then
            scrollVal = 18
        End If
        pagingDS.Clear()
        pagingAdapter.Fill(pagingDS, scrollVal, 5, "authors_table")
    End Sub
End Class

Tidak ada komentar:

Posting Komentar