datagrid分页,点了第二页再查询就出错!
出错的提示“无效的 CurrentPageIndex 值。它必须大于等于 0 且小于 PageCount。
不点其他的分页,查询没有问题,但如果点了其他页例如2,3...页,就出上面的错误了。
代码:
private void Button1_Click(object sender, System.EventArgs e)
{
SqlConnection myConnection = new SqlConnection("server=hp;uid='sa';password='sapoiuytrfg';database=wwww");
SqlDataAdapter myCommand = new SqlDataAdapter("select subject from tszl where subject like '%"+TextBox1.Text+"%'", myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds, "tszl");
Mydatagrid.DataSource = ds.Tables["tszl"].DefaultView;
Mydatagrid.DataBind();
Mydatagrid.Visible=true;
}
private void Mydatagrid_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
SqlConnection myConnection = new SqlConnection("server=hp;uid='sa';password='sapoiuytrfg';database=wwww");
SqlDataAdapter myCommand = new SqlDataAdapter("select subject from tszl where subject like '%"+TextBox1.Text+"%'", myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds, "tszl");
Mydatagrid.DataSource = ds.Tables["tszl"].DefaultView;
Mydatagrid.DataBind();
Mydatagrid.CurrentPageIndex = e.NewPageIndex;
Mydatagrid.DataBind();
}
不点其他的分页,查询没有问题,但如果点了其他页例如2,3...页,就出上面的错误了。
代码:
private void Button1_Click(object sender, System.EventArgs e)
{
SqlConnection myConnection = new SqlConnection("server=hp;uid='sa';password='sapoiuytrfg';database=wwww");
SqlDataAdapter myCommand = new SqlDataAdapter("select subject from tszl where subject like '%"+TextBox1.Text+"%'", myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds, "tszl");
Mydatagrid.DataSource = ds.Tables["tszl"].DefaultView;
Mydatagrid.DataBind();
Mydatagrid.Visible=true;
}
private void Mydatagrid_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
SqlConnection myConnection = new SqlConnection("server=hp;uid='sa';password='sapoiuytrfg';database=wwww");
SqlDataAdapter myCommand = new SqlDataAdapter("select subject from tszl where subject like '%"+TextBox1.Text+"%'", myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds, "tszl");
Mydatagrid.DataSource = ds.Tables["tszl"].DefaultView;
Mydatagrid.DataBind();
Mydatagrid.CurrentPageIndex = e.NewPageIndex;
Mydatagrid.DataBind();
}
...全文
请发表友善的回复…
发表回复
meixiaofeng 2004-06-25
- 打赏
- 举报
楼上的
cpp2017 2004-06-25
- 打赏
- 举报
Mydatagrid.CurrentPageIndex = e.NewPageIndex;
==> if(e.NewPageIndex>-1 && e.NewPageIndex<Mydatagrid.PageCount)
{
Mydatagrid.CurrentPageIndex = e.NewPageIndex;
Mydatagrid.DataBind();
}
zsms2 2004-06-25
- 打赏
- 举报
那是因为你在查询的时候查询结果没有第二页,而你现在的页索引还是1,所以会出错,如果你不是动态生成DATAGRID的话,你在点分页事件的时候,要把页索引置0
lwm110 2004-06-25
- 打赏
- 举报
因为你过滤掉了不符和条件的记录,已经不足两页了
你可以将datagrid页设为0或者判断当前页如果超出范围则减一一直到符合条件.
你可以将datagrid页设为0或者判断当前页如果超出范围则减一一直到符合条件.
gshope 2004-06-25
- 打赏
- 举报
你是不是在第一页删除数据了,已经不够第二页了吧?