Deleting records causes error with DataGrid paging.

Posted on 2005-04-14
Last Modified: 2010-04-07
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?

- Esulin
Question by:Esulin
    LVL 33

    Accepted Solution

    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,
    LVL 18

    Assisted Solution

    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;


    Author Comment


    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

    LVL 33

    Expert Comment

    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.
    LVL 18

    Expert Comment


    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..


    Author Comment

    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.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now