Razim Khan
Razim Khan

Reputation: 347

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

i want to bind my data Gridview but it throw an error. i allow paging when i click on new page e.g 1.2.3 then the above error throw here is my code. the one is page index change and the other one is my method of BindGrid. the error come when i click one new page 2 or 3 etc

updated code

 protected void DataGrid1_PageIndexChanged(Object sender, DataGridPageChangedEventArgs e)
    {

        DataGrid1.CurrentPageIndex = e.NewPageIndex;
        DataGrid1.DataSource = Session["data"] as DataTable;
        DataGrid1.DataBind();
    }
    private void BindGrid()
    {
        DataTable data = storedProcedureManager.sp_inactiveFiles(
                providerID,
                 days, CaseTypeID, CollectorID, user.UserRegID);
        lblMsg.Text = data.Rows.Count + " Record's Found.";
        log.Info(lblMsg.Text);
        Session["data"] = data;
        DataGrid1.DataSource = data;
        DataGrid1.DataBind();

        log.Info("Report Displayed.");

    }

Upvotes: 2

Views: 4305

Answers (2)

Kunal Vyas
Kunal Vyas

Reputation: 1

DataGrid1.DataSource = data;
DataGrid1.DataBind();
DataGrid1.CurrentPageIndex = 0;

Reset the CurrentPageIndex to 0 after the page has been bound.

Upvotes: 0

shreesha
shreesha

Reputation: 1881

Problem what is happening is when you fetch the data again in BindGrid() method you must be getting less number of pages.so you can do two things.

1)If you are filtering your data then reset the page index to 1.

protected void DataGrid1_PageIndexChanged(Object sender, DataGridPageChangedEventArgs e)
    {

        BindGrid();
    }
private void BindGrid()
    {
        DataTable data = storedProcedureManager.sp_inactiveFiles(
                providerID,
                 days,CaseTypeID,CollectorID,user.UserRegID);
        lblMsg.Text = data.Rows.Count + " Record's Found.";
        log.Info(lblMsg.Text);

        DataGrid1.DataSource = data;
        DataGrid1.CurrentPageIndex = 0;
        DataGrid1.DataBind();

        log.Info("Report Displayed.");

    }

2)If you are not filtering your data then store the datagrid value in a session object and use this for pageing.

 protected void Page_Load(object sender, EventArgs e)
    {
    if(!IsPostBack)
    {
    BindGrid();
    }

  }
protected void DataGrid1_PageIndexChanged(Object sender, DataGridPageChangedEventArgs e)
    {
        DataGrid1.CurrentPageIndex = e.NewPageIndex;
        DataGrid1.DataSource = Session["value"] as DataTable;
        DataGrid1.DataBind();
    }
private void BindGrid()
    {
        DataTable data = storedProcedureManager.sp_inactiveFiles(
                providerID,
                 days,CaseTypeID,CollectorID,user.UserRegID);
        lblMsg.Text = data.Rows.Count + " Record's Found.";
        log.Info(lblMsg.Text);
        Session["value"]=data;
        DataGrid1.DataSource = data;
        DataGrid1.DataBind();

        log.Info("Report Displayed.");

    }

Upvotes: 2

Related Questions