【问题标题】:DataGridView, ADO.NET, Binding, and Paging on the Client客户端上的 DataGridView、ADO.NET、绑定和分页
【发布时间】:2009-06-03 20:05:22
【问题描述】:

我正在开发一个 WinForm 应用程序,该应用程序有几个查询带回大约 20000 条记录,然后填充 DataTable,并将该 DataTable 绑定到 DataGridView。

我想让用户一次在网格中翻页 500 条记录。做这个的最好方式是什么?我想在客户端进行分页。我看到 System.Data.DataView 有一个过滤器和排序,但对分页没有任何帮助。

更新: 我开始考虑一些方法来做一些定制的事情。我可以获取 DataTable/DataRow 在内部使用的唯一“行计数 ID”吗?并用它来计算我需要的记录数?

【问题讨论】:

    标签: .net data-binding ado.net datatable pagination


    【解决方案1】:

    这是一个来自 VB-Tips 的示例:

    http://www.vb-tips.com/dbPages.aspx?ID=5dbe894a-a7e6-434c-bd84-73494c71063f

    Imports System.Data.SqlClient
    Imports System.Text
    Imports System.ComponentModel
    
    Public Class Form1
    
        Dim da As SqlDataAdapter
        Dim conn As SqlConnection
        Dim ds As New DataSet
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim strConn As String
            Dim cmd As SqlCommand
            Dim sbCmd As New StringBuilder
    
    
            strConn = String.Format("Server = {0};", Environment.MachineName)
            strConn &= "Database = NorthWind; Integrated Security = SSPI;"
            conn = New SqlConnection(strConn)
            cmd = New SqlCommand("Select count(ProductName) From Products", conn)
            Try
                da = New SqlDataAdapter("Select * from Products", conn)
    
                conn.Open()
    
                With nuPage
                    .Maximum = Math.Ceiling(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 nuPage_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nuPage.ValueChanged
            Dim intStart As Integer = (nuPage.Value - 1) * 10
            ds.Clear()
            da.Fill(ds, intStart, 10, "Products")
    
        End Sub
    End Class
    

    【讨论】:

    • 假设我正在管理 250 个 SQL 语句 - 现在使用这种方法,我必须为每个我想尝试的(总共 500 个 stmts)有一个“计数(*)风味”的 SQL 版本避免这种情况。 +1 向我展示了“da”的最小值,最大值猜测我从未放慢速度注意到该功能。
    【解决方案2】:

    我的第一次寻呼尝试涉及内置寻呼机。不过,我已经快速迁移到触发页面绑定的自定义元素。

    通常(至少在 ASP 中),您会将 DataTable 绑定到 DataGridView,然后执行

    DataGridView.ActivePageIndex=x; DataGridView.Databind();

    但是,由于我们谈论的是 20.000 条记录,因此最好先查找找到的记录数量,然后将 500 条记录带入客户端。

    *EDIT - 经过简单检查,Windows 窗体中没有分页选项。我最好的猜测是您需要进行查找/自定义分页。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      相关资源
      最近更新 更多