我用DATAGRID控件显示数据,没有问题;又做了翻页和查询也都好用,但翻完页再查询时就报错:   "无效的 CurrentPageIndex 值。它必须大于等于 0 且小于 PageCount。"
请哪位高手大侠给予指点,解释,多谢!!!多谢!!!!

解决方案 »

  1.   

    这很明白吧
    就是你翻页时将当前的页码赋给了大于1的数,页在你重新查询时,可能只能查出1页数据,页当前的页码索引还是大于1的数,当然出错啦
    你可以在查询时给datagrid的 CurrentPageIndex 重新赋值为0
    就可以啦
      

  2.   

    源码如下:sub button_click(sender as object,e as eventargs)
    dim i
    msg.text=""
    wzg.text=""
    for i=0 to cx.items.count-1
    if cx.items(i).selected then
    msg.text &=cx.items(i).text
    end if
    next
    msg.text &="='"&cxnr.text & "'"
    wzg.text &="select * from GSM基站配置数据表 where " 
    wzg.text &=msg.text
     dim conn as oledbconnection
    dim adpt as oledbdataadapter
    dim ds as dataset
    dim provider="provider=microsoft.jet.oledb.4.0"
    dim database="data source=" & server.mappath("gsmjzsj.mdb")
    conn=new oledbconnection(provider & ";"  & database)
    conn.open()
    adpt = new oledbdataadapter (wzg.text,conn)
    ds=new dataset()
    adpt.fill(ds,"GSM基站配置数据表")
    mydatagrid.datasource=ds.tables("GSM基站配置数据表").defaultview
    mydatagrid.databind()
    conn.close()  
    end subsub opendatabase_and_bindtodatagrid()
       
     dim conn as oledbconnection
    dim adpt as oledbdataadapter
    dim ds as dataset
    dim provider="provider=microsoft.jet.oledb.4.0"
    dim database="data source=" & server.mappath("gsmjzsj.mdb")conn=new oledbconnection(provider & ";"  & database)
    conn.open()dim sql="select * from GSM基站配置数据表 " 
    adpt = new oledbdataadapter ( sql,conn)
    'table.defaultview.sort = sortfield.text & sorttype.text
    ds=new dataset()
    adpt.fill(ds,"GSM基站配置数据表")
    ds.tables("GSM基站配置数据表").defaultview.sort= sortfield.text & sorttype.text
    MyDataGrid.datasource=ds.tables("GSM基站配置数据表").defaultview
    MyDataGrid.databind()
    conn.close()end sub
      

  3.   

    我以上已说过的啦
    你可以在查询绑定后这样
    MyDataGrid.datasource=ds.tables("GSM基站配置数据表").defaultview
    MyDataGrid.databind();
    加:MyDataGrid.CurrentPageIndex = 0;
      

  4.   

    在DataBind()之前先判断下
    #region 初始化DataGrid
    /// <summary>
    /// 初始化DataGrid
    /// </summary>
    private void iniData(DataTable dt)
    {
    int pagesum = 0;
    int intDataViewCount = dt.DefaultView.Count;
    int intDataGridPageSize = dgResult.PageSize;
    int intDataGridCurrentPageIndex = dgResult.CurrentPageIndex;
     
    if(intDataViewCount % intDataGridPageSize == 0)
    {
    pagesum = intDataViewCount/intDataGridPageSize;
    }
    else
    {
    pagesum = intDataViewCount/intDataGridPageSize + 1;
    }
     
    if(intDataViewCount == 0)
    {
    pagesum =1;
    }
     
    if(intDataGridCurrentPageIndex >= pagesum)
    {
    intDataGridCurrentPageIndex = pagesum-1;
    }
    dgResult.CurrentPageIndex = intDataGridCurrentPageIndex;
    }
    #endregion
      

  5.   

    如果你在翻页时改变了查询条件,就会出现这种问题。
    所以你必须在翻页前作查询,并判断查询后的记录数为多少,并求出有多少页,如果页码少于
    当前的页,则将当前页赋值为0.
    如下:
         Dim intCount, pageCount As Integer
                intCount = CInt(myds.Tables(0).Rows.Count)
                If intCount < 10 Then
                    pageCount = 1
                Else
                    If intCount Mod 10 <> 0 Then
                        pageCount = intCount \ 10 + 1
                    Else
                        pageCount = intCount / 10
                    End If
                End If            If pageCount = 1 Or dagInfo.CurrentPageIndex > pageCount - 1 Then
                    dagInfo.CurrentPageIndex = 0
                End If