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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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.Cou nt Mod dgdContactsList.PageSize = 1) Then
If dgdContactsList.CurrentPag eIndex >= dgdContactsList.PageCount - 1 And dgdContactsList.CurrentPag eIndex > 0 Then
dgdContactsList.CurrentPag eIndex -= 1
End If
End If
- Esulin.
If (dgdContactsList.Items.Cou
If dgdContactsList.CurrentPag
dgdContactsList.CurrentPag
End If
End If
- Esulin.
ASKER
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