Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

datagrid error: Invalid CurrentPageIndex value. It must be >= 0 and <

Status
Not open for further replies.

ISPrincess

Programmer
Feb 22, 2002
318
US
I knew it was too good to be true. I have a datagrid that i just implemented Datapaging on - and that worked just swell, until.....

You delete the only record on the last page.

Say I have 6 rows in my datagrid and my pagesize is set to 5. If I page to the row #6 (2nd page) and click on Delete which deletes the record from the table and the only record that was on that page, then I retrieve the data again and rebind the grid. I get the following error:


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


If, after a delete i just set the datagrid.currentpageindex = 0 that works fine, but I dont want to have to go to the first page anytime someone deletes a record.

Is there a way to easily set the datagrids.currentpageindex = datagrid.currentpageindex -1 whenever a user deletes the only record on the last page just before the databind?

PH
I was walking home one night and a guy hammering on a roof called me a paranoid little weirdo.
In morse code.
-Emo Phillips
 
Do it in a while - try - catch
Code:
bool ok = false;
int thePageIndex = 5;
while(!ok && thePageIndex >= 0)
{
  try
  {
    dg.CurrentPageIndex = thePageIndex;
    ok = true;
  }//try
  catch
  {
    thePageIndex--;
  }//catch
}//while
Most times, it will just set it and move on. If it doesn't, it'll keep decrementing until it finds something it can live with.

-paul

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Thanks. I found a little messy way of doing it.

First after any record deletion I hold onto the currentpageindex in m_CurrentPage, then I set dg.currentpageindex = 0.

After the databind I check to see if m_currentpage is > then the pagecount, if so, set dg.currentpageindex = pagecount.

Your way is sooooo much nicer though. I will attempt it.'

Thanks again!


PH
I was walking home one night and a guy hammering on a roof called me a paranoid little weirdo.
In morse code.
-Emo Phillips
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top