GridView分页无法按预期工作

时间:2013-05-10 16:42:55

标签: vb.net gridview

我从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

2 个答案:

答案 0 :(得分:0)

删除这些代码行

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

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

答案 1 :(得分: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)绑定