0

我从 vb.net 开始,我必须制作一个 GridView,它显示来自 sql 查询的寄存器,并在 GridView 上启用了分页。但是分页仅适用于第 1 页中的第一个寄存器,当我单击第 2 页时,它显示的寄存器数量少于应有的数量,并且带有页码的栏消失了。

我见过有类似问题的人,所说的只是:

 Protected Sub GridView2_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView2.PageIndexChanging
    GridView2.PageIndex = e.NewPageIndex
    GridView2.DataBind()
End Sub

但它似乎不起作用。

下面是用两个可能的查询填充 GridView 的代码。如果我禁用分页,它们工作得很好,所以问题不在于查询:

 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click


    Dim command As String
    command = "SELECT [GVA14]. [COD_CLIENT] As Codigo, [GVA14].[RAZON_SOCI] As Nombre,tabla.[periodo] As Cuota FROM [GVA14] LEFT JOIN (SELECT cod_client as cod_client ,MAX(periodo) as periodo FROM emCuotas GROUP BY cod_client) As tabla ON ([GVA14]. [COD_CLIENT] COLLATE DATABASE_DEFAULT=tabla.[cod_client] COLLATE DATABASE_DEFAULT) ORDER BY [GVA14].[COD_CLIENT]"
    Dim dTable As New DataTable
    Dim dataAdapter = New OleDbDataAdapter(command, con)

    dataAdapter.Fill(dTable)
    GridView2.DataSource = dTable
    GridView2.DataBind()
    nro = 1

End Sub

 Protected Sub Button4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button4.Click
    Dim f1 As String
    f1 = TextBox1.Text
    Dim f2 As String
    f2 = TextBox2.Text
    Dim command As String
    Dim cuantos As Integer = 50
    Dim inicio As Integer=0
    command = "SELECT COD_CLIENT,RAZON_SOCI,FECHA_ALTA FROM GVA14 WHERE FECHA_ALTA BETWEEN CONVERT(datetime,'" & f1 & "',103) AND CONVERT(datetime,'" & f2 & "',103) Order By COD_CLIENT,FECHA_ALTA"
    Dim dTable As New DataTable
    Dim dataAdapter = New OleDbDataAdapter(command, con)
    dataAdapter.Fill(dTable)
    GridView2.DataSource = dTable
    GridView2.DataBind()
    command = "SELECT Count(FECHA_ALTA) FROM GVA14 WHERE FECHA_ALTA BETWEEN CONVERT(datetime,'" & f1 & "',103) AND CONVERT(datetime,'" & f2 & "',103)"
    Dim comand As New Data.OleDb.OleDbCommand(command, con)

    Label1.Text = comand.ExecuteScalar
    nro=2

End Sub
4

2 回答 2

0

删除这些代码行

GridView2.PageIndex = e.NewPageIndex
GridView2.DataBind()

并且只指定GridView.PageSize(当然允许分页)

于 2013-05-10T16:51:25.743 回答
0

这是我所做的......

    Protected Sub gvExistingICByUser_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvExistingICByUser.PageIndexChanging
       Dim grid As GridView = DirectCast(sender, GridView)
       GetExistingICByUser(CurrentUser)
       grid.DataSource = dtExistingICByUser
       grid.PageIndex = e.NewPageIndex
       grid.DataBind()
    End Sub

PageIndexChanging 事件内的线路细分......
1) 将您的 gridview 转换为 grideview。(我不确定它是这样做的,但它可以工作而不是 w/o)

2)加载您的网格视图(我在另一个返回数据表的函数中执行此操作)

3)设置页面索引

4)绑定

于 2013-05-10T21:23:19.910 回答