请哪位高手帮我解决一下这个问题,
给我一个例子我学习学习,先谢了。

解决方案 »

  1.   

    例:
    private void Delete()
    {
    //count为选中(要删除)的记录数
    int count = 0;
    //执行删除操作
             for(int n=0;n<InterviewHistoryGrid.Items.Count;n++)
    {
    if(((CheckBox)myDataGrid.Items[n].FindControl("Check")).Checked)
    {
    //(这里执行你的删除操作)
                               ...
                               //
    //选中的记录加一
    ++ count;
    }
    }
    //判断是否选中一整页数据 而且当前页非首页
    if(count == myDataGrid.Items.Count && myDataGrid.CurrentPageIndex > 0 )
    {
    -- myDataGrid.CurrentPageIndex;
    }
    //绑定DataGrid
    myDataGridBind();
    }
      

  2.   

    上面有点问题private void Delete()
    {
    //count为选中(要删除)的记录数
    int count = 0;
    //执行删除操作
             for(int n=0;n<myDataGrid.Items.Count;n++)
    {
    if(((CheckBox)myDataGrid.Items[n].FindControl("Check")).Checked)
    {
    //(这里执行你的删除操作)
                               ...
                               //
    //选中的记录加一
    ++ count;
    }
    }
    //判断是否选中一整页数据 而且当前页非首页
    if(count == myDataGrid.Items.Count && myDataGrid.CurrentPageIndex > 0 )
    {
    -- myDataGrid.CurrentPageIndex;
    }
    //绑定DataGrid
    myDataGridBind();
    }
      

  3.   

    将 CurrentPageIndex 重设为 0 就行了
      

  4.   

    DataGrid翻页后删除记录出错的解决方案
    在论坛里见过不少网友问过这样的问题:当DataGrid翻到最后一页后,如果当前页的数据只有一条,那么删除这条记录就会出现“异常详细信息: System.Web.HttpException: 无效的 CurrentPageIndex 值。它必须大于等于 0 且小于 PageCount。”这样的错误,出现这个错误的原因是当最后一页上的这条记录被删除并重新绑定数据之后,DataGrid的总页数减少了一页,而它的CurrentPageIndex属性值却仍然是删除记录前最后一页的页索引值,这个值超出了它的总页数。一些网友解决这个问题的方法是:在删除记录之后立即将DataGrid的CurrentPageIndex设为0,即跳回第一页,这个方法虽然可以避免再发生这样的错误,然而却很不灵活,因为如果用户要删除最后一页上的两条记录,那么在删除了其中一条后,不得不重新点击分页按钮进入最后一页来删除第二条,而且很多时候这种方法会给用户带来困扰,因为很容易把跳转到的第一页的数据当成是正执行删除命令的最后一页的数据,其实这个问题有更好的解决办法,如果我们用的是DataGrid的内建的删除功能,即一次只能删除一条记录的话,那么在它的DeleteCommand事件处理程序中,可以通过下面的方法,来实现当最后一页没有数据时,自动跳回前一页,否则只需要重新绑定数据即可:void DataGrid1_Delete(object src,DataGridCommandEventArgs e){
    //执行删除命令,从数据库或其它保存数据的地方删除DataGrid中对应的记录(略)
    //如果当前页上只有一条数据并且DataGrid的当前页索引大于0,那么就让DataGrid返回上一页
    if(DataGrid1.Items.Count==1&&DataGrid1.CurrentPageIndex>0){
    DataGrid1.CurrentPageIndex--;
    }
    //重新绑定数据
    BindData();
    }如果用了模板列中的CheckBox,实现一次删除多条选中的记录的话,那么可以在删除按钮的Click事件处理程序中这样来实现上面的功能:
    void deleteButton_Click(object src,EventArgs e){
    int deletedRows=0; //记录删除掉的行数
    foreach(DataGridItem item in DataGrid1.Items){
    if(item.ItemType==ListItemType.Item||item.ItemType==ListItemType.AlternatingItem){
    CheckBox ckbox=item.FindControl("delckbox") as CheckBox;
    if(ckbox!=null&&ckbox.Checked){
    deletedRows++;
    //从数据库或其它保存数据的地方删除DataGrid中对应的记录(略)
    }
    }
    }
    //如果当前页的记录总数等于已经被删除掉的记录数并且DataGrid当前页索引大于0,那么让DataGrid返回上一页
    if(deletedRows==DataGrid1.Items.Count&&DataGrid.CurrentPageIndex>0){
    DataGrid1.CurrentPageIndex--;
    }
    //重新绑定数据
    BindData();
    }
      

  5.   

    减少两页就可以了
    if(DataGrid1.Items.Coun==0&&DataGrid.CurrentPageIndex>0){
    DataGrid1.CurrentPageIndex = DataGrid1.CurrentPageIndex-2;
    }
      

  6.   

    简单一点,在DeleteCommand事件中加入如下代码: 
    if(dg_Goods.PageCount-dg_Goods.CurrentPageIndex==1 && dg_Goods.Items.Count==1)
    {
    if(dg_Goods.PageCount>1)
    {
    dg_Goods.CurrentPageIndex=dg_Goods.CurrentPageIndex-1;
    }
    else
    {
    dg_Goods.CurrentPageIndex=0;
    }
    }
      

  7.   

    if(dgShow.Items.Count==1)
    {
         if(dgShow.CurrentPageIndex!=0)
         dgShow.CurrentPageIndex = dgShow.CurrentPageIndex-1;
    }
    string Sql = "delete from tbStudentinfo where studentid="+e.Item.Cells[0].Text+"";楼主你所碰到的错误我曾经也碰到过
    只要你加上一个if语句就OK了
    如果你还没有解觉的话就加我QQ:274787137
      

  8.   

    当删除DataGrid最后页的所有记录时,保证分页不会出错的2种代码
    http://nhlinkin.cnblogs.com/articles/125426.html
      

  9.   

    在你邦定DataGrid的时候,在邦定是将DataGrid1.CurrentPageIndex=0