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