Link to home
Start Free TrialLog in
Avatar of danimbrogno
danimbrogno

asked on

Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount.

Hello, i have a simple product catalogue in which the user enters a search word and the server returns a list of possible matches. I also have paging enabled in this datagrid. When you page to say.. page 4, and then search for a different term, if the new term does not fill 4 pages, it throws this error. How can i reset the page index to 1, whenever the user searches for a new key word?
ASKER CERTIFIED SOLUTION
Avatar of raterus
raterus
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just do

    YourGrid.CurrentPageIndex = 0

to set it back to page 1.
You can check to see how many rows you have in the DataSet tables and then enable disable the paging:
Assuming I set paging for more than 20 items then

If MyDataSet.Tables["MyTable"].Rows.Count < 20 then

MyDataGrid.AllowPaging = false

else

MyDataGrid.AllowPaging = true

end if

MyDataGrid.DataSource = MyDataSet.Tables["MyTable"];
MyDataGrid.DataBind();

Always remember to enable/disable the paging before you bind the datagrid.

Best, nauman
raterus is correct here is an example:

 Private Sub SearchProducts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchProducts.Click
        Dim Query As String
        Query = "SELECT * FROM sschecter.JobNumbers JN WHERE "
        Select Case FieldsToMatch.SelectedValue
            Case "JobNumber"
                Query &= "JN.JobNumber LIKE '%" & SearchQuery.Text & "%'"
            Case "CustomerID"
                Query &= "JN.CustomerID LIKE '%" & SearchQuery.Text & "%'"
            Case "Either"
                Query &= "JN.JobNumber LIKE '%" & SearchQuery.Text & "%' " & _
                "OR JN.CustomerID LIKE '%" & SearchQuery.Text & "%'"
        End Select
        Query &= " ORDER BY JN.JobNumber DESC"
        dg.CurrentPageIndex = 0    ******************************************
        BindTheData(Query)
        FullQuery.Value = Query
    End Sub

Regards,

Aeros