[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

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
0
Esulin
Asked:
Esulin
  • 2
  • 2
  • 2
2 Solutions
 
raterusCommented:
I ran into this problem too, it's pretty rare to happen, but you can fix it with this little trick right before you call .DataBind()

            Dim itemCount As Integer = yourDataSource.ItemCount 'get this however you want
            Dim pageindex As Integer = yourDataGrid.CurrentPageIndex
            Dim pagesize As Integer = yourDataGrid.PageSize
            Dim dgItemCount As Integer = pagesize * pageindex

            If itemCount <= dgItemCount Then
                yourDataGrid.CurrentPageIndex -= 1
            End If

Hope this helps,
--Michael
0
 
tusharashahCommented:
After you delete Record ( and before binding again) try to do something like following:

if( e.Item.ItemIndex == DataGrid1.Items.Count && (e.Item.ItemIndex%10) == 1)
{
       DataGrid1.CurrentPageIndex = DataGrid1.CurrentPageIndex - 1;
}

-tushar
0
 
EsulinAuthor Commented:
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
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
raterusCommented:
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.
0
 
tusharashahCommented:
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
0
 
EsulinAuthor Commented:
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.
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now