我有一个启用分页的数据网格。我根据过滤条件在数据网格中显示结果。我已经过滤了数据,现在它有 2 页。当我转到第 2 页时。我正在再次执行搜索功能以缩小结果范围。然后我收到一个错误,例如“无效的 CurrentPageIndex 值。它必须 >= 0 并且 < PageCount+datagrid 分页”我确信第二次搜索只会产生比前一次更少的页数。如何解决这个问题?提前致谢
5 回答
当您进行某些更改时,您需要重置到第 1 页。这包括过滤更改。几乎,每当您更改网格可能可用的行数时,请返回第 1 页。
我有一个启用分页的数据网格。我根据过滤条件在数据网格中显示结果。我已经过滤了数据,现在它有 2 页。当我转到第 2 页并再次执行搜索功能以缩小结果范围时。然后我收到一个错误
“无效的 CurrentPageIndex 值。它必须 >= 0 并且 < PageCount+datagrid 分页”
我确信第二次搜索只会产生比前一次更少的页数。如何解决这个问题呢 ?错误显示:
当前页面索引值。它必须 >= 0 并且 < PageCount。
我解决了问题
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;
这是我用来解决问题的行。
另一个建议是仅在 PageCount 已更改并导致 HttpException 时重置 CurrentPageIndex。代码片段基于 Les Smith 的示例。
Try
dataGrid1.DataBind()
Catch
' We possibly don't have the correct PageCount.
dataGrid1.CurrentPageIndex = 0
dataGrid1.DataBind()
End Try
您可以转到第一页或捕获异常并移至您喜欢的任何页面。如果您要从最后一页删除一条记录,您可能希望移至上一条记录。
try
{
grid.DataSource = dao.PopulateGrid();
grid.DataBind();
}
catch
{
if (grid.CurrentPageIndex >= grid.PageCount)
{
grid.CurrentPageIndex -= 1;
grid.DataSource = dao.PopulateGrid();
grid.DataBind();
}
}
就我而言,我所做的是每次在数据网格控件上加载的数据发生更改时,始终应用重置当前页面索引的行。
DataGrid.CurrentPageIndex = 0
DataGrid.DataSource = 数据表/数据集
DataGrid.DataBind()
这是因为将数据源绑定到数据网格时引发的异常并非总是页数不一致。