private bool HandleDeleteCallback(int affectedRows, Exception ex) {
            GridViewDeletedEventArgs sea = new GridViewDeletedEventArgs(affectedRows, ex);
            sea.SetKeys(_deleteKeys);
            sea.SetValues(_deleteValues);

            OnRowDeleted(sea);
            _deleteKeys = null;
            _deleteValues = null;

            if (ex != null && !sea.ExceptionHandled) {
                // If there is no validator in the validation group that could make sense
                // of the error, return false to proceed with standard exception handling.
                // But if there is one, we want to let it display its error instead of throwing.
                if (PageIsValidAfterModelException()) {
                    return false;
                }
            }
            EditIndex = -1;

            if (affectedRows > 0) {
                // Patch up the current page.  We might have deleted the last records on the last page, so move
                // to the right page.
                int rowCount = (int)ViewState[ItemCountViewStateKey];
                // Can't have negative rowCount
                int expectedRowCount = Math.Max(0, rowCount - affectedRows);

                if (AllowPaging) {
                    // Calculate the expected page count
                    int expectedPageCount = 0;
                    checked {
                        // Correctly calculate the expectedPageCount. Special case: We want there to always be at least one page even if there are no items
                        // so that we can always *safely* calculate the pageindex as expectedRowCount - 1
                        expectedPageCount = Math.Max(1, (expectedRowCount + PageSize - 1) / PageSize);
                    }

                    // Adjust the pageIndex based on the expected page count
                    _pageIndex = Math.Min(_pageIndex, expectedPageCount - 1);
                    Debug.Assert(_pageIndex >= 0, "Page index should never be negative!");
                }

                if (SelectedIndex >= 0) {
                    if (expectedRowCount == 0) {
                        // There is nothing to select
                        SelectedIndex = -1;
                    }
                    else {
                        // Calculate the selected index in terms of the row number in the total rows
                        int selectedRow = AllowPaging ? (PageIndex * PageSize) + SelectedIndex : SelectedIndex;
                        // If the selected index is no longer valid make it the last index
                        if (selectedRow > expectedRowCount) {
                            int lastIndex = AllowPaging ? expectedRowCount % PageSize : expectedRowCount;
                            SelectedIndex = lastIndex;
                        }
                    }
                }
            }
            _deletedRowIndex = -1;

            RequiresDataBinding = true;
            return true;
        }
 private bool HandleDeleteCallback(int affectedRows, Exception ex)
 {
     GridViewDeletedEventArgs e = new GridViewDeletedEventArgs(affectedRows, ex);
     e.SetKeys(this._deleteKeys);
     e.SetValues(this._deleteValues);
     this.OnRowDeleted(e);
     this._deleteKeys = null;
     this._deleteValues = null;
     if (((ex != null) && !e.ExceptionHandled) && this.PageIsValidAfterModelException())
     {
         return false;
     }
     this.EditIndex = -1;
     if (affectedRows > 0)
     {
         int num = (int) this.ViewState["_!ItemCount"];
         int num2 = Math.Max(0, num - affectedRows);
         if (this.AllowPaging)
         {
             int num3 = 0;
             num3 = Math.Max(1, ((num2 + this.PageSize) - 1) / this.PageSize);
             this._pageIndex = Math.Min(this._pageIndex, num3 - 1);
         }
         if (this.SelectedIndex >= 0)
         {
             if (num2 == 0)
             {
                 this.SelectedIndex = -1;
             }
             else
             {
                 int num4 = this.AllowPaging ? ((this.PageIndex * this.PageSize) + this.SelectedIndex) : this.SelectedIndex;
                 if (num4 > num2)
                 {
                     int num5 = this.AllowPaging ? (num2 % this.PageSize) : num2;
                     this.SelectedIndex = num5;
                 }
             }
         }
     }
     this._deletedRowIndex = -1;
     base.RequiresDataBinding = true;
     return true;
 }