【问题标题】:Datagrid Paging : Invalid CurrentPageIndex value. It must be >= 0数据网格分页:无效的 CurrentPageIndex 值。它必须 >= 0
【发布时间】:2009-03-20 04:01:19
【问题描述】:

我有一个启用分页的数据网格。我根据过滤条件在数据网格中显示结果。我已经过滤了数据,现在它有 2 页。当我转到第 2 页时。我正在再次执行搜索功能以缩小结果范围。然后我收到一个错误,例如“无效的 CurrentPageIndex 值。它必须 >= 0 并且

【问题讨论】:

    标签: datagrid paging


    【解决方案1】:

    当您进行某些更改时,您需要重置到第 1 页。这包括过滤更改。几乎每次您更改网格可能可用的行数时,请返回第 1 页。

    【讨论】:

      【解决方案2】:

      我有一个启用分页的数据网格。我根据过滤条件在数据网格中显示结果。我已经过滤了数据,现在它有 2 页。当我转到第 2 页并再次执行搜索功能以缩小结果范围时。然后我收到类似

      的错误

      "无效的 CurrentPageIndex 值。它必须 >= 0 并且

      我确信第二次搜索只会产生比前一次更少的页数。如何解决这个问题呢 ? 错误显示:

      CurrentPageIndex 值。它必须 >= 0 并且

      我解决了问题

      protected void btnSearchLibrary_Click(object sender, EventArgs e)
      {
          if(!String.IsNullOrEmpty(txtSearchLibraryNo.Text.Trim()))
          oBookReceiptDTO.LibraryCardNo = txtSearchLibraryNo.Text.Trim();
          gvBooksReceiptList.CurrentPageIndex = 0;
          FillGridViewBookReceiptList(oBookReceiptDTO);
      }
      

      注意:gvBooksReceiptList.CurrentPageIndex = 0; 这是我用来解决问题的行。

      【讨论】:

        【解决方案3】:

        另一个建议是仅在 PageCount 已更改并导致 HttpException 时重置 CurrentPageIndex。代码片段基于 Les Smith 的example

            Try
                dataGrid1.DataBind()
            Catch
                ' We possibly don't have the correct PageCount.
                dataGrid1.CurrentPageIndex = 0
                dataGrid1.DataBind()
            End Try
        

        【讨论】:

          【解决方案4】:

          您可以转到第一页或捕获异常并移至您喜欢的任何页面。如果您要从最后一页删除一条记录,您可能希望移至上一条记录。

           try
              {
                 grid.DataSource = dao.PopulateGrid();
                 grid.DataBind();
              }
               catch
              {
               if (grid.CurrentPageIndex >= grid.PageCount)
                {
                  grid.CurrentPageIndex -= 1;
                  grid.DataSource = dao.PopulateGrid();
                  grid.DataBind();
                }
              }
          

          【讨论】:

            【解决方案5】:

            就我而言,我所做的是每次在 Data Grid 控件上加载的数据发生更改时,始终应用重置当前页面索引的行。

            DataGrid.CurrentPageIndex = 0

            DataGrid.DataSource = 数据表/数据集

            DataGrid.DataBind()

            这是因为将数据源绑定到数据网格时引发的异常并非总是页数不一致。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-06-12
              • 2023-02-23
              • 2015-02-26
              • 2019-01-05
              • 1970-01-01
              相关资源
              最近更新 更多