我在做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。
请问这是怎么回事啊?
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。
请问这是怎么回事啊?
DataGrid1.CurrentPageIndex=0;
if(DataGrid1.CurrentPageIndex<0 || DataGrid1.CurrentPageIndex>PageCount)
{
DataGrid1.CurrentPageIndex=0;
}PageCount:总页数
名称“PageCount”在类或命名空间“_04510523._9_1”中不存在
DataGrid1.CurrentPageIndex=0;