解决方法:
在数据绑定中加:
if (DataGrid1.CurrentPageIndex > (dtList.Rows.Count - 1) / DataGrid1.PageSize)
DataGrid1.CurrentPageIndex = (dtList.Rows.Count - 1) / DataGrid1.PageSize;
例如:
private void showData()
{
#region 数据绑定
string positionList = "select * from users ";
DataTable dtList = CommonApp.getCompanyInfo(positionList).Tables[0];
count.Text = "合计:" + dtList.Rows.Count.ToString();
if (DataGrid1.CurrentPageIndex > (dtList.Rows.Count - 1) / DataGrid1.PageSize)
DataGrid1.CurrentPageIndex = (dtList.Rows.Count - 1) / DataGrid1.PageSize;
DataGrid1.DataSource = dtList;
DataGrid1.DataBind();
dtList.Dispose();
#endregion
}