GridView分页VB.NET桌面应用程序 [英] GridView paging VB.NET Desktop application

查看:75
本文介绍了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


这篇关于GridView分页VB.NET桌面应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆