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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting last row in a datagrid cause an Error

Status
Not open for further replies.

123ASP

MIS
Nov 2, 2002
239
0
0
US
HI, I am note sure why I am getting such error, but whenI got to the last row in a datagrid, and delete it,I get the following errror.Please advice for any help.
thanks
Al

Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount.

Source Error:


Line 41: MyCommand.Fill(DS, &quot;investor&quot;)
Line 42: dg.DataSource=DS.Tables(&quot;investor&quot;).DefaultView
Line 43: dg.DataBind()
Line 44: ShowPageInformation()
Line 45: End Sub

 
Set the current page index to 0

dg.CurrentPageIndex = 0 after the databind to guarantee you will be on teh first page.

regards,
Brian
The problem with doing something right the first time is that nobody appreciates how difficult it was.
 
Brian is slightly mistaken.
If you put dg.CurrentPageIndex = 0 after the databind nothing happens as the grid has already been bound with a current page index value other than 0. Make sure the line is BEFORE the databind line.

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Hay Mark, your point corrected the issue. If I have 3 records per page. and I have total pages of 9. Every time I delete a record, the dg.CurrentPageIndex = 0 will take me to the first page. Here what I want to do , if I am at page 9 of 9 and I have 3 records in every page, once I delete a record, I will stay in the page untill the last record is deleted ( 3rd record ) Once the 3rd record is deleted, I will go back one page and the datagrid will show page 8.

Is this correct code to apply the above senario

if dg.CurrentPageIndex <> dg.PageCount then
dg.CurrentPageIndex = dg.CurrentPageIndex -1
end if

I am not sure if this will give me the above result.
thanks for your help.
Al
 
hmm you've almost got it. problem is that the grid doesn't know the record has been deleted till you bind it, which causes an error if the page doesn't exist anymore.

This should work i think (haven't tested it)

//check to see if your on the last page note-pagecount is not 0 based

if(dg.CurrentPageIndex == dg.PageCount -1)
{
//check to see if the page is gone now

if((dg.Items.Count - 1) == (dg.PageSize * (dg.PageCount - 1)))
{
dg.CurrentPageIndex = dg.CurrentPageIndex -1;
}
}

That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Hi - FWIW - I had this problem before and spent too much time on this - I think I tried some of the above solutions, to no avail.

My solution was to put the DataBind in a Try-Catch block and when the error occurred, I just repeated the DataBind - that worked out fine. But you might want to try the previous suggestions.
 
I spend a good time trying to solve it. Finallay I get fedup.
The last try was the following:

if dg.currentPageIndex < dg.PageCount and dg.CurrentPageIndex >=1 then
dg.CurrentPageIndex =dg.CurrentPageIndex -1
else
dg.CurrentPageIndex =dg.CurrentPageIndex -1
end if

BindData(ViewState(&quot;SortExpr&quot;))

The above code removed the error &quot;Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount. &quot;

Bue on the other hand, everytime I delete a record, CurrentPage go back (if I were at page 4 it goes to page3)
Guys, come on let try to Solve this issue.

Thanks
Al
 
Correction:

if dg.currentPageIndex < dg.PageCount and dg.CurrentPageIndex >=1 then
dg.CurrentPageIndex =dg.CurrentPageIndex -1
else
dg.CurrentPageIndex =dg.CurrentPageIndex
end if
 
hi DLLHell,

I was not able to fix the problem, I read your solution, what do you mean by " I just repeated the DataBind "

If your solution worked , please advice.
thanks

Al
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top