ASP.NET Gridview Paging Problem

Brian picture Brian · Feb 23, 2009 · Viewed 7.8k times · Source

I have a gridview that is databound in the code-behind using a stored procedure. I am handling the Paging event in the code as well, but whenever I click on a page number, I keep getting the empty data template instead of more rows. Any suggestions?

EDIT: I am re-binding the data source of the gv after I change the page index.

Here is my code - I have a dropdown list that determines what the data source is:

Protected Sub ddlProjectForm_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlProjectForm.SelectedIndexChanged
    Dim strProjectFormID As String = Me.ddlProjectForm.SelectedValue
    Dim conn As New SqlConnection(WebConfigurationManager.ConnectionStrings("Conn").ConnectionString)
    Dim cmd As New SqlCommand()
    Dim da As New SqlDataAdapter
    Dim ds As New DataSet

    If strProjectFormID <> "Select" Then
        Try
            Using conn
                conn.Open()

                With cmd
                    .Connection = conn
                    .CommandType = CommandType.StoredProcedure
                    .CommandText = "sp_GetAllFormData"
                    .Parameters.AddWithValue("@projectFormID", strProjectFormID)
                End With

                da.SelectCommand = cmd
                da.Fill(ds)

                Me.gvAllSentData.DataSource = ds.Tables(0)
                Me.gvAllSentData.DataBind()
                Me.gvAllSentData.Visible = True
            End Using
        Catch sqlEx As SqlException
            Dim newError As New ErrorLogger(Me.Page.Title, sqlEx.Message, Session("UserName"))
            newError.LogError()

            Trace.Write(sqlEx.Message)
            Me.lblBadFeedback.Visible = True
            Me.lblBadFeedback.Text = "We're sorry - an error has occurred.  It has been logged and will be reviewed by the site admin."
        Catch ex As Exception
            Dim newError As New ErrorLogger(Me.Page.Title, ex.Message, Session("UserName"))
            newError.LogError()

            Trace.Write(ex.Message)
            Me.lblBadFeedback.Visible = True
            Me.lblBadFeedback.Text = "We're sorry - an error has occurred.  It has been logged and will be reviewed by the site admin."
        End Try
    Else
        Me.gvAllSentData.DataSource = Nothing
        Me.gvAllSentData.Visible = False
    End If

End Sub

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

Answer

Gavin Miller picture Gavin Miller · Feb 23, 2009

You are rebinding an empty datasource. Your code should read:

Protected Sub gvAllSentData_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvAllSentData.PageIndexChanging
    Me.gvAllSentData.PageIndex = e.NewPageIndex
    Me.gvAllSentData.DataSource = __The_Data_To_Bind__
    Me.gvAllSentData.DataBind()
End Sub