我在做DataGrid的查询的时候,发生了一个错误,实在想不出是什么原因?在此请高手帮忙!
     private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
DataGrid1_d();
}
这是我在分页的时候的代码,
private void DataGrid1_d()
{
SqlConnection conn = new SqlConnection((string)System.Configuration.ConfigurationSettings.AppSettings["ss"]);
conn.Open();
string sqlstr = "select * from student";
SqlDataAdapter da = new SqlDataAdapter(sqlstr,conn);
DataSet ds=new DataSet();
da.Fill(ds,"dt");
this.DataGrid1.DataSource=ds.Tables["dt"].DefaultView;
this.DataGrid1.DataBind();
}
这个是绑定DataGrid,我运行之后,在DropDownList里选择ID点查询,显示正常,但是当我把DataGrid翻到其他页的时候,再选一个ID点查询的时候就出错了!
错误提示是:
           无效的 CurrentPageIndex 值。它必须大于等于 0 且小于 PageCount。 
  请问这是怎么回事啊?

解决方案 »

  1.   

    你可以这样:在点击查询的时候加入一句代码,把DataGrid的页码设置为第一页
    DataGrid1.CurrentPageIndex=0;
      

  2.   

    判断一下
    if(DataGrid1.CurrentPageIndex<0 || DataGrid1.CurrentPageIndex>PageCount)
    {
    DataGrid1.CurrentPageIndex=0;
    }PageCount:总页数
      

  3.   

    它提示:
           名称“PageCount”在类或命名空间“_04510523._9_1”中不存在
      

  4.   

    这是因为你每次查询的结果页数不一样造成的,所以每次查询的时候最好初始化一下当前页,
    DataGrid1.CurrentPageIndex=0;