DataGrid的分页问题!大家看看遇到过没有! 在删除完成后重新绑定DataGrid.类似的情况在你改变PageSize后必须要保证有足够的pages存在 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 问题是如何解决!我想除非在邦定前手动去设置新的currentpageindex if(DataGrid1.CurrentPageIndex!=0){if (DataGrid1.Items.Count==1) DataGrid1.CurrentPageIndex -=1;} 在绑定前加入:if (DataGrid1.Items.Count == 1){ DataGrid1.CurrentPageIndex--;} 感谢您使用微软产品。这要看您的datagrid的数据源如何更新了。您可以在datagrid的数据源更新时检查一下CurrentPageIndex是否超出了PageCount。或者您可以在PageIndexChanged事件的处理函数中对e.NewPageIndex做一下检查,这要视您代码的情况而定:if (e.NewPageIndex < this.dataGrid1.PageCount){ dataGrid1.CurrentPageIndex = e.NewPageIndex; dataGrid1.DataBind();}======================- 微软全球技术中心本贴子仅供CSDN的用户作为参考信息使用。其内容不具备任何法律保障。您需要考虑到并承担使用此信息可能带来的风险。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。====================== 不对吧。比如说有6条记录,每页是5条。在第二页删除一条之后(也就是在DeleteCommand中用SqlCommand等来删除),这时候由于还没有执行DataGrid1.DataBind,所以,PageCount还是2,而CurrentPageIndex=1,但是现在如果直接执行DataBind,那肯定就是错误的了。因为实际上PageCount只有1。这里有个解决方法,就是在绑定前判断:if (DataSet1.Tables[0].Rows.Count == DataGrid1.PageSize * DataGrid1.CurrentPageIndex){ DataGrid1.CurrentPageIndex --;}DataGrid1.DataBind(); 楼上的方法只能解决删除一条记录时的操作,但是如果数据库中的数据发生了变化,例如被其他用户删除了7条记录。这时肯定还会出错!我觉得这个问题比较严重,微软.net本身没有一个解决方法!只能靠自己去判断 感谢您使用微软产品。这个问题要最终解决,必须在每次DataGrid进行数据绑定时检查currentIndex是否出界。在所有需要执行DataGrid1..DataBind()的地方调用下面的函数:private void DataGridBind(DataGrid _dataGrid){ int currentIndex = _dataGrid.CurrentPageIndex; _dataGrid.CurrentPageIndex = 0; _dataGrid.DataBind(); if (currentIndex < _dataGrid.PageCount) { _dataGrid.CurrentPageIndex = currentIndex; _dataGrid.DataBind(); } }欢迎继续讨论!======================- 微软全球技术中心本贴子仅供CSDN的用户作为参考信息使用。其内容不具备任何法律保障。您需要考虑到并承担使用此信息可能带来的风险。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。====================== To icyer::) 相互切磋,共同进步!上面的问题在于DataGrid的DataSource何时更新。当DataGrid的DataSource不变时不需要对currentIndex进行检查,而当DataSource改动后进行检查即可。谢谢你的支持! 这样棒定两次是不是浪费一些资源!我的方法是:每次棒定之前根据DataView.Count 取得的最新的记录数目和 DataGrid.PageSize来计算实际的页数然后与 dataGrid.CurrentPageIndex进行比较根据需要来重新设置 DataGrid.CurrentPage 属性,然后进行帮定,这样只需要帮定一次! if(DataGrid1.CurrentPageIndex!=0) { if(DataGrid1.Items.Count==0) { DataGrid1.CurrentPageIndex-=1; } } BindGrid(); 如何让button响应事件在page_load前执行? 关于div里标签解析的问题 网络编程 高手,这个怎么判断 ? GridView的问题··· 急在先等!! 在线等!!!!怎样把image控件中显示的ftp上的图片保存到数据库中 请教Asp.net 2.0 与asp 程序同在一个目录,怎样才能使其都能正常运行? 关于关闭浏览器页面的时候想对数据库进行操作 100分!一个关于tree的问题,急!!!!! about radioButton, is it a bug ? 如何在服务器控件按钮的点击事件里把某个变量的值post给另外一个aspx程序? 各位大哥,帮小弟解决一下这个问题。给50分
if(DataGrid1.CurrentPageIndex!=0)
{
if (DataGrid1.Items.Count==1)
DataGrid1.CurrentPageIndex -=1;
}
if (DataGrid1.Items.Count == 1)
{
DataGrid1.CurrentPageIndex--;
}
感谢您使用微软产品。这要看您的datagrid的数据源如何更新了。您可以在datagrid的数据源更新时检查一下CurrentPageIndex是否超出了PageCount。
或者您可以在PageIndexChanged事件的处理函数中对e.NewPageIndex做一下检查,这要视您代码的情况而定:if (e.NewPageIndex < this.dataGrid1.PageCount)
{
dataGrid1.CurrentPageIndex = e.NewPageIndex;
dataGrid1.DataBind();
}======================
- 微软全球技术中心本贴子仅供CSDN的用户作为参考信息使用。其内容不具备任何法律保障。您需要考虑到并承担使用此信息可能带来的风险。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
======================
比如说有6条记录,每页是5条。在第二页删除一条之后(也就是在DeleteCommand中用SqlCommand等来删除),这时候由于还没有执行DataGrid1.DataBind,所以,PageCount还是2,而CurrentPageIndex=1,但是现在如果直接执行DataBind,那肯定就是错误的了。因为实际上PageCount只有1。
这里有个解决方法,就是在绑定前判断:
if (DataSet1.Tables[0].Rows.Count == DataGrid1.PageSize * DataGrid1.CurrentPageIndex)
{
DataGrid1.CurrentPageIndex --;
}
DataGrid1.DataBind();
private void DataGridBind(DataGrid _dataGrid)
{
int currentIndex = _dataGrid.CurrentPageIndex;
_dataGrid.CurrentPageIndex = 0;
_dataGrid.DataBind();
if (currentIndex < _dataGrid.PageCount)
{
_dataGrid.CurrentPageIndex = currentIndex;
_dataGrid.DataBind();
}
}欢迎继续讨论!======================
- 微软全球技术中心本贴子仅供CSDN的用户作为参考信息使用。其内容不具备任何法律保障。您需要考虑到并承担使用此信息可能带来的风险。具体事项可参见使用条款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
======================
:) 相互切磋,共同进步!上面的问题在于DataGrid的DataSource何时更新。当DataGrid的DataSource不变时不需要对currentIndex进行检查,而当DataSource改动后进行检查即可。谢谢你的支持!
我的方法是:每次棒定之前根据DataView.Count 取得的最新的记录数目和 DataGrid.PageSize来计算实际的页数然后与 dataGrid.CurrentPageIndex进行比较根据需要来重新设置 DataGrid.CurrentPage 属性,然后进行帮定,这样只需要帮定一次!
{
if(DataGrid1.Items.Count==0)
{
DataGrid1.CurrentPageIndex-=1;
}
}
BindGrid();