teledextri
teledextri

Reputation: 125

GridView1_PageIndexChanging not changing when index changing

I am trying to display 50 at a time. It should activate when the "Page Index" is changed. The original Gridview works and has more than 1000 entries. The GridView1_PageIndexChanging or GridView1.PageIndex = e.NewPageIndex is not functioning. Here is my code:

    <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

Upvotes: 1

Views: 2883

Answers (1)

Josh Darnell
Josh Darnell

Reputation: 11433

The GridView will handle your paging for you, if you let it. You don't need to filter the query yourself. I have a couple of suggestions:

Centralize your databinding code
Put all that databinding code in a centralized function. This will make your PageIndexChanging event look cleaner, and reduce the risk of typo errors (if you have to duplicate that code elsewhere). It should be something like this:

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

Note: I took out your calls to Dispose, as they were redundant (that's what the Using block does)

Update your PageIndexChanging code to call that sub
Now you can have a cleaned up function for the paging:

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

Optimization: cache your database call in some way
You shouldn't need to retrieve 1,000+ rows from your database every time you page your GridView. Put the DataTable in a Session variable the first time you load the Grid (in Page_Load, I imagine):

Session("myGridViewData") = dt

Then grab it out of there any time you need it again (such as during paging):

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

Upvotes: 1

Related Questions