[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 737
  • 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 ???

Thanks

Alain
0
alain_matos
Asked:
alain_matos
1 Solution
 
mangesh004Commented:
Hi,

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.


Regards,
Mangesh
0
 
Sammy AgeilCommented:
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
else
myGrid.CurrentPageIndex = e.NewPageIndex
End If

bind your grid after this condition

HTH
0
 
Sammy AgeilCommented:
oops, dont forget to change mygrid to your datagrid id

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
0
 
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
0
 
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)

  DeleteUsedCar(IdAuto)
  ' 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
 else
  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
 MyGrid.DataBind

0
 
Sammy AgeilCommented:
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
http://www.tek-tips.com/viewthread.cfm?qid=1085900&page=8

HTH
0
 
GRCHELPDESKCommented:
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,
Maurice
0
 
alain_matosAuthor Commented:
Thank  you GRCHELPDESK  for your comments

Alain
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

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