我准备每页显示25行,用的是access数据库
主要后台代码很简单,使这样的,
private void Page_Load(object sender, System.EventArgs e)
{
this.Button1.Click += new System.EventHandler(this.NavigateToPage);
this.Button2.Click += new System.EventHandler(this.NavigateToPage);
this.Button3.Click += new System.EventHandler(this.NavigateToPage);
this.Button4.Click += new System.EventHandler(this.NavigateToPage);



where = "Colid="+Request.QueryString["cid"];
le = Request.QueryString["le"];
if (!IsPostBack)
MyConnection("Select * from CommInfo where "+where+" ORDER BY PubTime DESC");
}
private void MyConnection(string MySql)
{


OleDbConnection conn = new OleDbConnection(Application["connstr"].ToString());

conn.Open(); OleDbCommand comm=new OleDbCommand(MySql,conn);

OleDbDataReader dr= comm.ExecuteReader();
dr.Close();
dr= comm.ExecuteReader();
if(dr.HasRows==true)
{
DataGrid1.DataSource = dr;
DataGrid1.DataBind();
} dr.Close();

conn.Close();
}
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex=e.NewPageIndex;
MyConnection("Select * from CommInfo where "+where+" ORDER BY PubTime DESC");


}

private void NavigateToPage(object sender, System.EventArgs e)
{
string PageInfo  = ((Button)sender).CommandName;
switch (PageInfo)
{
case "首页":
DataGrid1.CurrentPageIndex = 0;
break;
case "上一页":
if (DataGrid1.CurrentPageIndex > 0)
DataGrid1.CurrentPageIndex -= 1;
break;
case "下一页":
if (DataGrid1.CurrentPageIndex < (DataGrid1.PageCount - 1))
DataGrid1.CurrentPageIndex += 1;
break;
case "末页":
DataGrid1.CurrentPageIndex = (DataGrid1.PageCount - 1);
break;
}
MyConnection("Select * from CommInfo where "+where+" ORDER BY PubTime DESC");


}可我运行时,显示出了前20条记录,但是点下一页刷新后还是这20条 请教高手帮我修改

解决方案 »

  1.   

    一个分页的例子http://dotnet.aspx.cc/ShowDetail.aspx?id=B12283DE-DB20-4322-ACCC-12724442808A
      

  2.   

    执行完 case "下一页":
    if (DataGrid1.CurrentPageIndex < (DataGrid1.PageCount - 1))
    DataGrid1.CurrentPageIndex += 1;
    后面加上重新绑定数据源的代码:
    DataGrid1.DataSource = dr;
    DataGrid1.DataBind();
      

  3.   

    我一共是30条记录 我设置的是每页显示25条
    我运行后显示的是25条  那我点下一页刷新后应该显示另外5条 可实际上还是显示的前25条,
    我看了那个例子了 没什么不妥阿
    在下一页加DataGrid1.DataBind();还是不行
      

  4.   

    我发现问题所在了
    就是DataGrid1.PageCount竟然是0 那就肯定不会执行DataGrid1.CurrentPageIndex += 1;那就永远显示第一页 这是那里出问题了阿 郁闷~~~~~~~~~~~~
    再说我的datagrid怎么可能是零页呢
      

  5.   

    说了半天
    我还是不懂
    到底要用datagrid的那一个属性来确定一页显示多少行那?
    大哥门说明一下啊
    小弟谢谢了!