Click here to Skip to main content
16,018,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Is there anyway for make paging for gridview data in vb.net desktop application instead of automatic scrolling ?
Posted

1 solution

Refer:
http://www.vb-tips.com/PageDataGridView.aspx[^]
http://forums.devx.com/showthread.php?155210-Paging-DataSet-in-grid-view-for-desktop-application[^]

Quote: This method of paging a DataGridView uses an overload of the DataAdapter.fill to control which records are loaded. You can use this with the NumericUpDown which is needed for this and the DataGridView is used to switch which page of data is shown.

VB
Imports System.Data.SqlClient
Imports System.ComponentModel
Public Class Form1
    Private ds As New DataSet
    Private da As New SqlDataAdapter
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim strConn = String.Format("Server = {0};", "YourServerName")
        strConn &= "Database = NorthWind; Integrated Security = SSPI;"
        Dim conn As New SqlConnection(strConn)
        Dim cmd As New SqlCommand("Select count(ProductName) From Products", conn)
        Try
            da = New SqlDataAdapter("Select * from Products", conn)
            conn.Open()
            With NumericUpDown1
                .Maximum = Math.Ceiling(CInt(cmd.ExecuteScalar) \ 10)
                .Minimum = 1
                .Increment = 1
                .Value = 1
            End With
            conn.Close()
            da.Fill(ds, 0, 10, "Products")
            ds.Tables("Products").DefaultView.AllowNew = False
            DataGridView1.DataSource = ds.Tables("Products")
            For Each col As Object In DataGridView1.Columns
                If TypeOf col Is DataGridViewCheckBoxColumn Then
                    DirectCast(col, DataGridViewCheckBoxColumn).Visible = False
                ElseIf TypeOf col Is DataGridViewTextBoxColumn Then
                    Dim tbc As DataGridViewTextBoxColumn = CType(col, DataGridViewTextBoxColumn)
                    If tbc.Name = "ProductName" Then
                        tbc.Width = 275
                        tbc.HeaderText = "Product Name"
                    ElseIf tbc.Name = "UnitPrice" Then
                        tbc.Width = 75
                        tbc.HeaderText = "Price"
                        tbc.DefaultCellStyle.Format = "c"
                        tbc.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
                    Else
                        tbc.Visible = False
                    End If
                End If
            Next
        Catch ex As Exception
            Trace.WriteLine(ex.ToString)
        End Try
    End Sub
    Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles NumericUpDown1.ValueChanged
        Dim intStart As Integer = CInt((NumericUpDown1.Value - 1) * 10)
        ds.Clear()
        da.Fill(ds, intStart, 10, "Products")
    End Sub
End Class
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900