上面是源代码,他分页显示出来了,但我点击第二页的时候,他就报无效的 CurrentPageIndex 值。它必须大于等于 0 且小于 PageCount。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Web.HttpException: 无效的 CurrentPageIndex 值。它必须大于等于 0 且小于 PageCount。源错误: 
行 147: {
行 148: DataGrid1.CurrentPageIndex = e.NewPageIndex;
行 149: DataGrid1.DataBind();     
行 150:
行 151:  
 源文件: c:\inetpub\wwwroot\testpage2\webform1.aspx.cs    行: 149 堆栈跟踪: 
[HttpException (0x80004005): 无效的 CurrentPageIndex 值。它必须大于等于 0 且小于 PageCount。]
   System.Web.UI.WebControls.DataGrid.CreateControlHierarchy(Boolean useDataSource)
   System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e)
   System.Web.UI.WebControls.BaseDataList.DataBind()
   testPage2.WebForm1.DataGrid1_PageIndexChanged(Object source, DataGridPageChangedEventArgs e) in c:\inetpub\wwwroot\testpage2\webform1.aspx.cs:149
   System.Web.UI.WebControls.DataGrid.OnPageIndexChanged(DataGridPageChangedEventArgs e)
   System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e)
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
   System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e)
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
   System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   System.Web.UI.Page.ProcessRequestMain() 
请各位大哥帮我看看,问题出在那里

解决方案 »

  1.   

    在每改变CurrentPageIndex后,必须重新邦定datagrid
      

  2.   

    DataGrid1.CurrentPageIndex = e.NewPageIndex;
    DataGrid1.DataBind();    
    这个地方有个严重的问题,你的数据源呢?你需要把数据源传递过来!
    dataSet11 = (DataSet1)Cache["ds"];
    DataGrid1.CurrentPageIndex = e.NewPageIndex;
    DataGrid1.DataBind();     
      

  3.   

    if (!this.Page.IsPostBack)
    {    
    this.sqlDataAdapter1.Fill(dataSet11);
    this.DataGrid1.DataBind(); 
    }
    需要该一点
    if (!this.Page.IsPostBack)
    {    
         this.sqlDataAdapter1.Fill(dataSet11);
         this.DataGrid1.DataBind();
         Cache["ds"] =  dataSet11 ;
    }
      

  4.   

    DataGrid1.DataSource=dataSet11;
    DataGrid1.DataMember=dataSet11.Tables[0].ToString();
    DataGrid1.AllowPaging = true;
    DataGrid1.PagerStyle.Mode =PagerMode.NumericPages;
    DataGrid1.PagerStyle.PageButtonCount = 2;
    DataGrid1.PageSize = 2;
    this.sqlDataAdapter1.Fill(dataSet11);
    this.DataGrid1.DataBind(); 写到一块,放进一个方法里,比如叫之为GridBind();
    在page_load方法里
    if(!Page.IsPostBack)
    this.GridBind();
    在DataGrid1_PageIndexChanged里
    DataGrid1.CurrentPageIndex = e.NewPageIndex;
    this.GridBind();
      

  5.   

    to: tavor(龙双公子) 
    按照你的方法,错误是不抱了,但点击下一页的时候,datagrid一片空白,没有显示了
      

  6.   

    楼主,你的代码太乱了!我看你还是先好好整理整理你的代码·这样就算有错也比较容易找出原因!其实分页代码很简单! protected System.Web.UI.WebControls.DataGrid DataGrid1;
    protected string szConn;
    protected SqlConnection cn; private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    szConn=ConfigurationSettings.AppSettings["Forum"];
    cn = new SqlConnection(szConn);
    if(!IsPostBack)
    {
    BindGrid();
    }
    } #region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
    this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion public void BindGrid()
    {
    string szSql = "select threadID , threadName from Threads";
    SqlDataAdapter adp = new SqlDataAdapter(szSql,cn);
    DataSet ds = new DataSet();
    adp.Fill(ds);
    DataGrid1.DataKeyField = "threadID";
    DataGrid1.DataSource = ds.Tables[0].DefaultView;
    DataGrid1.DataBind(); }
    private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    DataGrid1.CurrentPageIndex = e.NewPageIndex;
    BindGrid();
    }
      

  7.   

    你仔细看看的事件里是不是这么写的,在DataGrid1_PageIndexChanged里DataGrid1.CurrentPageIndex = e.NewPageIndex;
    this.GridBind();呵呵,这个是肯定可用的 ^_^
      

  8.   

    不过也有可能是你设置的这些属性出的问题
    DataGrid1.AllowPaging = true;
    DataGrid1.PagerStyle.Mode =PagerMode.NumericPages;
    DataGrid1.PagerStyle.PageButtonCount = 2;
    DataGrid1.PageSize = 2;
    你别在运行时设置这些东西,特别是AllowPaging,你在属性生成器里设置这
      

  9.   

    不过据我的实验,不会是设置属性的事,呵呵根据我的经验来推论,应该是DataGrid1_PageIndexChanged事件里少写了
    this.GridBind();
      

  10.   

    推荐用这个免费分页控件,可以定制样式而且不用那么麻烦:http://www.webdiyer.com