无效的CurrentPageIndex值。它必须是> = 0且< PageCount

时间:2015-08-21 12:18:37

标签: asp.net datagridview

我想绑定我的数据Gridview,但它会引发错误。  我点击新页面时允许分页,例如1.2.3然后上面的错误抛出 这是我的代码。 一个是页面索引更改,另一个是我的BindGrid方法。 当我点击一个新页面2或3等时出现错误

更新代码

 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.");

    }

2 个答案:

答案 0 :(得分:2)

问题是当你在BindGrid()方法中再次获取数据时,你必须获得更少的页面数量。所以你可以做两件事。

1)如果您要过滤数据,请将页面索引重置为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)如果您没有过滤数据,则将datagrid值存储在会话对象中,并将其用于分页。

 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.");

    }

答案 1 :(得分:0)

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

页面绑定后,将CurrentPageIndex重置为0。