Question

I have a datagrid with paging enabled. I am displaying the results in datagrid based on a filtering condition. I have filtered the data and it has now 2 pages. when i go to 2 nd page. and i am doing the seacrhing function once again to narrow down the results. Then I am getting an error like "Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount+datagrid paging" I am sure that the second search will produce only less number of pages than the previous one. How to solve ths problem ? Thanks in advance

Was it helpful?

Solution

When you make certain changes, you need to reset to page 1. That includes filtering changes. Pretty much, any time you change the number of rows that might be available to your grid, go back to page 1.

OTHER TIPS

I have a datagrid with paging enabled. I am displaying the results in datagrid based on a filtering condition. I have filtered the data and it has now 2 pages. When I go to 2nd page and I am doing the searching function once again to narrow down the results. Then I am getting an error like

"Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount+datagrid paging"

I am sure that the second search will produce only less number of pages than the previous one. How to solve this problem ? Error show:

CurrentPageIndex value. It must be >= 0 and < the PageCount.

I solved the problem

protected void btnSearchLibrary_Click(object sender, EventArgs e)
{
    if(!String.IsNullOrEmpty(txtSearchLibraryNo.Text.Trim()))
    oBookReceiptDTO.LibraryCardNo = txtSearchLibraryNo.Text.Trim();
    gvBooksReceiptList.CurrentPageIndex = 0;
    FillGridViewBookReceiptList(oBookReceiptDTO);
}

NOTE: gvBooksReceiptList.CurrentPageIndex = 0; this is the line I used to solve the problem.

Another suggestion is to only reset the CurrentPageIndex when the PageCount has changed and causes the HttpException. The code fragment is based on Les Smith's example.

    Try
        dataGrid1.DataBind()
    Catch
        ' We possibly don't have the correct PageCount.
        dataGrid1.CurrentPageIndex = 0
        dataGrid1.DataBind()
    End Try

You can either go to the first page or catch the exception and move to whatever page you like. If you are deleting one record from the last page, you might want to move to the previous one.

 try
    {
       grid.DataSource = dao.PopulateGrid();
       grid.DataBind();
    }
     catch
    {
     if (grid.CurrentPageIndex >= grid.PageCount)
      {
        grid.CurrentPageIndex -= 1;
        grid.DataSource = dao.PopulateGrid();
        grid.DataBind();
      }
    }

For my case, what I did is to always apply the line resetting the current page index every time there is a change of data that is being loaded on the Data Grid control.

DataGrid.CurrentPageIndex = 0

DataGrid.DataSource = Datatable/Dataset

DataGrid.DataBind()

This is because it is not all the time that the exception thrown when binding a data source to the Data Grid would be the inconsistent page count.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top