GridView1_PageIndexChanging在索引更改时不更改

时间:2014-01-02 11:33:27

标签: asp.net vb.net gridview

我试图一次显示50个。它应该在“页面索引”更改时激活。原始的Gridview可以工作,并且有超过1000个条目。 GridView1_PageIndexChanging或GridView1.PageIndex = e.NewPageIndex无效。 这是我的代码:

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="50" OnPageIndexChanging="GridView1_PageIndexChanging">
    </asp:GridView>

  Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs) Handles GridView1.PageIndexChanging
    GridView1.PageIndex = e.NewPageIndex
    Dim connStr, cmdStr As String
    Dim myDataSet As New DataSet
    Dim dt As New DataTable()
    connStr = "connection string works"
    cmdStr = "SELECT * FROM table1;"
    Try
        Using conn As New SqlConnection(connStr)
            Using cmd As New SqlCommand(cmdStr, conn)
                conn.Open()
                cmd.ExecuteNonQuery()
                Using myDataAdapter As New SqlDataAdapter(cmd)
                    myDataAdapter.Fill(myDataSet)
                    dt = myDataSet.Tables(0)
                    Dim filteredSet = dt.AsEnumerable().Skip((GridView1.PageIndex - 1) * 50)
                    GridView1.DataSource = filteredSet
                    GridView1.DataBind()
                End Using
                conn.Close()
                cmd.Dispose()
                conn.Dispose()
            End Using
        End Using
    Catch ex As Exception

    End Try
 End Sub

1 个答案:

答案 0 :(得分:1)

如果您愿意,GridView将为您处理您的分页。您不需要自己过滤查询。我有几点建议:

集中您的数据绑定代码
将所有数据绑定代码放在集中函数中。这将使您的PageIndexChanging事件看起来更清晰,并降低拼写错误的风险(如果您必须在其他地方复制该代码)。它应该是这样的:

Private Sub BindGrid()
    Dim connStr, cmdStr As String
    Dim myDataSet As New DataSet
    Dim dt As New DataTable()
    connStr = "connection string works"
    cmdStr = "SELECT * FROM table1;"
    Try
        Using conn As New SqlConnection(connStr)
            Using cmd As New SqlCommand(cmdStr, conn)
                conn.Open()
                cmd.ExecuteNonQuery()
                Using myDataAdapter As New SqlDataAdapter(cmd)
                    myDataAdapter.Fill(myDataSet)
                    dt = myDataSet.Tables(0)
                    GridView1.DataSource = dt
                    GridView1.DataBind()
                End Using
                conn.Close()
            End Using
        End Using
    Catch ex As Exception

    End Try
End Sub

注意:我接到了对Dispose的来电,因为它们是多余的(这就是Using阻止所做的事情)

更新您的PageIndexChanging代码以调用该子
现在,您可以为分页设置清理功能:

Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs) Handles GridView1.PageIndexChanging
    GridView1.PageIndex = e.NewPageIndex
    BindGrid() 
End Sub

优化:以某种方式缓存您的数据库调用
每次打开GridView时,都不需要从数据库中检索1,000多行。在第一次加载Grid时将DataTable放入Session变量中(在Page_Load中,我想象):

Session("myGridViewData") = dt

然后在你再次需要它时(例如在分页期间)将它从那里拿出来:

Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs) Handles GridView1.PageIndexChanging
    GridView1.PageIndex = e.NewPageIndex
    Gridview1.DataSource = CType(Session("myGridViewData"), DataTable)
    GridView1.DataBind()        
End Sub