views:

182

answers:

4

I have a datagrid where a row can be deleted (using ajax). I'm have problem with the pager in the following scenario:

Lets say my PageSize is 10, I have 101 rows so that's 11 pages with the last page with an single element. Let no assume that I'm on page 10 (PageIndex=9) and delete a row. Then I go to the 11'th page (who's now empty and doesn't really exist). ASP now shows me the EmptyDataTemplate and no pager so I can't go back.

My approach (which isn't working) is to detect this scenario and step one page back:

public void Bind()
{
    gridMain.DataBind();
}

public void SetPage(int page)
{
    gridMain.PageIndex = page;
    gridMain.DataBind();
}

protected void ldsGridMain_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
    selectArgs = e;
    e.Result = (new EnquiryListController()).GetEnquiryList(OnBind(this), supplier);
}

protected void ldsGridMain_Selected(object sender, LinqDataSourceStatusEventArgs e)
{
    totalRows = selectArgs.Arguments.TotalRowCount;

    //Detect if we need to update the page: 
    if (gridMain.PageIndex > 0 && (gridMain.PageSize * gridMain.PageIndex + 1) > totalRows) SetPage(gridMain.PageIndex - 1);
}

protected void gridMain_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    SetPage(e.NewPageIndex);    
}

I can see that SetPage is called with the the right page index, but the databind doesn't seem to called as I still get the EmptyDataTemplate.

A: 

SetPage(gridMain.PageIndex - 1) might be incorrect. You have to calculate what the last page will be for the current row count. Current PageIndex-1 can be out of range.

Viktor Jevdokimov
well I'm not getting any Exceptions so I don't think that's it.
Niels Bosma
Not getting exceptions doesn't mean you have tested all scenarios. I just found this code might lead to exception, when PageIndex-1 is out of range. Also, PageSize*PageIndex+1 also may be incorrect:PageSize = 10, PageIndex = 2 (3rd page), 10*2+1=21, not 30 as you expecting.
Viktor Jevdokimov
PageSize * PageIndex + 1 is actually what I want as I want to compare with totalRows. If PageSize * PageIndex + 1 is bigger than totalRows I know I have a page that hasn't any rows. And need to go back one page (it can at most be one page that needs to step back as there need to be at most one element on last page for it to exists.) The problem is in the last DataBind() that doesn't have anty effect.
Niels Bosma
+3  A: 

When you delete the item, you need to rebind the data, so that the pager resizes.
Not just call databind, you have to reset the datasource and call databind. It doesn't keep the data in the datasource between post backs.

Before you do that, make sure you reset the pageIndex to one that is in scope on the new set.

DevelopingChris
I can't as I "remove" the elements using ajax.
Niels Bosma
asp.net built in controls and ajax don't always play nice, you might have to use my absolute most despised thing, an update panel, to pull off the best of both worlds.you could also just use jqGrid or some other client paging grid and call it quits on asp.net built in controls all together.
DevelopingChris
A: 

Rebind the grid in SetPage with updated datasource and set pageindex. check if RecordCount/PageSize is less than NumberofPages in pager or current Page index then decrease the page index as per RecordCount/PageSize .

Himadri
A: 

Can't you just use something like:

public void SetPage(int page)
{
    gridMain.PageIndex = Math.Max(page, gridMain.PageCount);
    gridMain.DataBind();
}
Stobor