GridView分页VB.NET桌面应用程序
问题描述:
反正有没有在vb.net桌面应用程序中对gridview数据进行分页而不是自动滚动的方法?
Is there anyway for make paging for gridview data in vb.net desktop application instead of automatic scrolling ?
答
引用:
http://www.vb-tips.com/PageDataGridView.aspx [ http://forums.devx.com/showthread. php?155210-Paging-DataSet-in-grid-view-for-desktop-application [
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.
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