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

J

Juan G.

I have a DataGrid in which I use paging, as well as Delete buttons. My
program works perfectly, except when I attempt to delete the only item on the
page; I have no trouble deleting an item when there are more than one item
on a page. For example, I have a list that is three pages long; there is
currently only one item on page 3. I receive the above error message when I
try to delete this item. Do I need to include a special line of code to
prevent this from happening?
 
J

Juan G.

Thank you, Teemu. I read this thread, and I think I might understand. I code
in VB.Net, so the C# answer was a little confusing. They mentioned this code:

if ((DataGrid.Items.Count % DataGrid.PageSize) == 1
&& DataGrid.CurrentPageIndex != 0 )
DataGrid.CurrentPageIndex = DataGrid.PageCount - 2;

Would it be correct to translate this as:

If (DataGrid.Items.Count OR DataGrid.PageSize = 1
AND DataGrid.CurrentPageIndex NotEqual 0 ) Then
DataGrid.CurrentPageIndex = DataGrid.PageCount - 2

Also, do I place this command at the beginning of my Delete Row sub (BEFORE
I delete the row)?

Thanks again for your help.

Juan G.
 
J

Juan G.

I just did some research, and I think the correct VB.NET syntax would be:

If ((Math.IEEERemainder(DataGrid.Items.Count, DataGrid.PageSize)=1) AndAlso
(DataGrid.CurrentPageIndex <>0)) Then DataGrid.CurrentPageIndex =
DataGrid.PageCount-2

End If
 
T

Teemu Keiski

Hello,

it's crucial that you just have correct CurrentPageIndex when the grid is
rebound again. So, if deleting would fail then you wouldn't change the
index, but if it succeeds and you rebind the list, then you'd want to
correct the index.
 
T

Teemu Keiski

And by the way

if ((DataGrid.Items.Count % DataGrid.PageSize) == 1

means modulus e.g

If (DataGrid.Items.Count Mod DataGrid.PageSize) = 1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top