Link to home
Start Free TrialLog in
Avatar of Esulin
Esulin

asked on

Deleting records causes error with DataGrid paging.

I'm using a DataGrid control with paging.

Say that I have 10 records per page. If I have 21 records, there will only be one record on the 3rd page. If I delete that record there is no more records on that page and I get an error back from the datagrid "Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount" as can be expected.

I tried comparing datagrid.pagecount to datagrid.currentPageIndex just before I call the datagrid.DataBind, but it looks like the pagecount is only calculated during the databind (which makes sense).

So, without going through the hassle of manually calculating how many records I have in my dataset compared to my number of pages/number or items per page... How do I make my page go to the previous page when the last record on the current page is deleted?

Regards,
- Esulin
ASKER CERTIFIED SOLUTION
Avatar of raterus
raterus
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Esulin
Esulin

ASKER

Tushar,

Your solution looks good although the problem is that I call the bind through a procedure which does not recieve any arguments.

Raterus, I figured that would be one way to do it, but I was looking for something a bit more elegant. :P

Regards,
-Esulin
I don't feel you are going to get much more elegant that that.  I'd almost categorize your issue as a borderline bug in the DataGrid, though in reality it's just doing what you told it to do.  Idealy if it detects that it can't successfully render the current page, it should fall back to the previous page.  You could extend the datagrid and add this functionality to the .DataBind method, unfortunately the only problem with that is you must know the datasource count beforehand.  If you are binding from a sqldatareader, you will not know this.
Esulin,

It really dosnt matter if its different function.. But, first if you can show us the DataGrid Binding Function and Delete function then we will understand your case better and guide you thru appropriate solution..

-Tushar
Avatar of Esulin

ASKER

I figured out a good enough solution. I've used the following snippet of code in my Delete function BEFORE I delete the record:

                If (dgdContactsList.Items.Count Mod dgdContactsList.PageSize = 1) Then
                    If dgdContactsList.CurrentPageIndex >= dgdContactsList.PageCount - 1 And dgdContactsList.CurrentPageIndex > 0 Then
                        dgdContactsList.CurrentPageIndex -= 1
                    End If
                End If

- Esulin.