• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 749
  • Last Modified:

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

Hi everybody

I Have a problem whith a datagrid, when I finish to delete all the items in a page

A error is display( Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount )

This happen in the event "PageIndexChanged"

Does anyone have a solution for this error ???


1 Solution

I got your problem. but  i have some questions.

wht logic you are using to select the rows for deletion .? Do you have a datagrid with checkboxes on every row? or you have just one button which deletes all the record in the table.

Explain me whts ther in your case?

How many records do you have and how many recrds you are displaying per page.

try this in the datagrid deletecommand event

If myGrid.Items.Count MOD myGrid.PageSize = 1 AND _
myGrid.CurrentPageIndex = myGrid.PageCount - 1 AND _
myGrid.CurrentPageIndex <> 1 THEN
myGrid.CurrentPageIndex = myGrid.CurrentPageIndex - 1
myGrid.CurrentPageIndex = e.NewPageIndex
End If

bind your grid after this condition

oops, dont forget to change mygrid to your datagrid id

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

alain_matosAuthor Commented:
I have a delete bouton per each row, when the bouton is clicked the row is deleted.

I have 25 Items per page

Thanks for your solution. I have not tested yet. I,ll write you later to let you know

alain_matosAuthor Commented:
I have a delete bouton per each row, when the bouton is clicked the row is deleted.

I have 25 Items per page
alain_matosAuthor Commented:
Hi sammy1971

I tried your solutions, but id did not work, I'll show my code lines

Sub MyGrid_Delete(sender As Object, e As DataGridCommandEventArgs)
  dim IdAuto as integer=GridUserCars.DataKeys(e.Item.ItemIndex)

  ' I call a delete function to delete in the DataBase
  If MyGrid.Items.Count MOD MyGrid.PageSize = 1 AND _
   MyGrid.CurrentPageIndex = MyGrid.PageCount - 1 AND _
   MyGrid.CurrentPageIndex <> 1 THEN
   MyGrid.CurrentPageIndex = MyGrid.CurrentPageIndex - 1
  MyGrid.CurrentPageIndex =e.NewPageIndex
  ' There's an error in this line e.newPageIndex is not a member of  DataGridCommandEventArgs
 End If

MyGrid.DataSource=GetGridSource 'Function that returns a dataset

Ok now I know you are using asp.net 2.0
take a look at this post and it will show you how to resolve this issue

This sounds like a problem I had a while back.  I'd get that message when deleting records from my datagrid.  
It would happen because of the paging feature combined with my pageload event.  I'll explain that.

If I was on the last page of the datagrid (lets say page 3) and deleted the files, page 3 no longer existed.
So when it posted back, on page load I was binding to the data to the grid, and it was looking for page 3.
Since there weren't enough records to create this page 3, I'd get that error message.
Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount

I had to reset the CurrentPageIndex to correct it.  Something like:
dgResults.CurrentPageIndex = 0

I kept getting the error and didn't understand why.  After setting some breakpoints I realized that
the pageload was firing before the event where I reset page index.  So I had to stop binding the data
to the grid in the pageload.

I hope that helps,
alain_matosAuthor Commented:
Thank  you GRCHELPDESK  for your comments

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now