在删除完成后重新绑定DataGrid.类似的情况在你改变PageSize后必须要保证有足够的pages存在

解决方案 »

  1.   

    问题是如何解决!我想除非在邦定前手动去设置新的currentpageindex
      

  2.   


    if(DataGrid1.CurrentPageIndex!=0)
    {
    if (DataGrid1.Items.Count==1) 
    DataGrid1.CurrentPageIndex -=1;
    }
      

  3.   

    在绑定前加入:
    if (DataGrid1.Items.Count == 1)
    {
      DataGrid1.CurrentPageIndex--;
    }
      

  4.   


    感谢您使用微软产品。这要看您的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))。
    ======================
      

  5.   

    不对吧。
    比如说有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();
      

  6.   

    楼上的方法只能解决删除一条记录时的操作,但是如果数据库中的数据发生了变化,例如被其他用户删除了7条记录。这时肯定还会出错!我觉得这个问题比较严重,微软.net本身没有一个解决方法!只能靠自己去判断
      

  7.   

    感谢您使用微软产品。这个问题要最终解决,必须在每次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))。
    ======================
      

  8.   

    To icyer:
    :) 相互切磋,共同进步!上面的问题在于DataGrid的DataSource何时更新。当DataGrid的DataSource不变时不需要对currentIndex进行检查,而当DataSource改动后进行检查即可。谢谢你的支持!
      

  9.   

    这样棒定两次是不是浪费一些资源!
    我的方法是:每次棒定之前根据DataView.Count 取得的最新的记录数目和 DataGrid.PageSize来计算实际的页数然后与 dataGrid.CurrentPageIndex进行比较根据需要来重新设置 DataGrid.CurrentPage 属性,然后进行帮定,这样只需要帮定一次!
      

  10.   

    if(DataGrid1.CurrentPageIndex!=0)
    {
    if(DataGrid1.Items.Count==0)
    {
    DataGrid1.CurrentPageIndex-=1;
    }
    }
    BindGrid();