因为删除DataGrid第三页的最后一条记录时,CurrentPageIndex的值还为三,而你的记录只分成两页,你应该在删除以后把CurrentPageIndex改成1或0

解决方案 »

  1.   

    Private Sub DataGrid1_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.DeleteCommand    Dim key As String = DataGrid1.DataKeys(e.Item.ItemIndex).ToString()
        Dim r As dsCategories.CategoriesRow
        r = DsCategories1.Categories.FindByCategoryID(key)    lbName.Text = r.Item(1).ToString()
        lbDescription.Text = r.Item(2).ToString()
        lbId.Text = r.Item(0).ToString()
        r.Delete()    SqlDataAdapter1.Update(DsCategories1)
      DataGrid1.CurrentPageIndex=0
        DataGrid1.DataBind()
    End Sub试试,我机器上没有DOTNET无法帮你调试,只能用感觉去写
      

  2.   

    做个判断
    if(DataGrid1.CurrentPageIndex>0)
    {
       DataGrid1.CurrentPageIndex=0;
    }
    else
    {
       DataGrid1.DataBind();
    }
      

  3.   

    if ((MyDataGrid.CurrentPageIndex!=0)&&((int)E.Item.ItemIndex==0))
    {
     if (MyDataGrid.Items.Count==1)   MyDataGrid.CurrentPageIndex-=1;
    }
    BindDataGrid();
      

  4.   

    不用这么麻烦,其余照常,datagrid有删除时加上
    try
    {
    DataGrid1.DataBind();
    }
    catch
    {
    DataGrid1.CurrentPageIndex=this.DataGrid1.PageCount-1;
    DataGrid1.DataBind();
    }
    就可以了
      

  5.   

    不太懂vb 给你个c# 参考一下吧解决删除最后页出错问题int page;
    if(myds.Tables["news"].DefaultView.Count%mydg.PageSize==0)
    page=myds.Tables["news"].DefaultView.Count/mydg.PageSize;
    else
    page=myds.Tables["news"].DefaultView.Count/mydg.PageSize+1;
    if(myds.Tables["news"].DefaultView.Count==0)
    page=1;
    if(mydg.CurrentPageIndex>=page)
    mydg.CurrentPageIndex=page-1;
      

  6.   

    孟子的挺全。
    <%@ Page Language="vb" AutoEventWireup="false" Codebehind="DataGridPaging.aspx.vb" Inherits="DataGridPaging"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
      <HEAD>
        <meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
        <meta content="JavaScript" name="vs_defaultClientScript">
        <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
      </HEAD>
      <body MS_POSITIONING="GridLayout">
        <form id="Form1" runat="server">
          <asp:datagrid id="MyDataGrid" runat="server" AlternatingItemStyle-BackColor="#eeeeee"
       HeaderStyle-BackColor="#aaaadd" Font-Size="8pt" Font-Name="Verdana" CellSpacing="0"
        CellPadding="3" GridLines="Both" BorderWidth="1" BorderColor="black"
     OnPageIndexChanged="MyDataGrid_Page" PagerStyle-HorizontalAlign="Right"
      PagerStyle-Mode="NumericPages" PageSize="5" AllowPaging="True">
            <AlternatingItemStyle BackColor="#EEEEEE"></AlternatingItemStyle>
            <HeaderStyle BackColor="#AAAADD"></HeaderStyle>
            <PagerStyle HorizontalAlign="Right" Mode="NumericPages"></PagerStyle>
          </asp:datagrid>
          <p style="font-size:9pt">
            <asp:label id="lblPageCount" runat="server"></asp:label>&nbsp;
            <asp:label id="lblCurrentIndex" runat="server"></asp:label>
            <asp:linkbutton id="btnFirst" onclick="PagerButtonClick" runat="server"
     Font-Name="verdana" Font-size="8pt" ForeColor="navy" CommandArgument="0">
     </asp:linkbutton>&nbsp;
            <asp:linkbutton id="btnPrev" onclick="PagerButtonClick" runat="server"
     Font-Name="verdana" Font-size="8pt" ForeColor="navy" CommandArgument="prev">
     </asp:linkbutton>&nbsp;
            <asp:linkbutton id="btnNext" onclick="PagerButtonClick" runat="server"
     Font-Name="verdana" Font-size="8pt" ForeColor="navy" CommandArgument="next">
     </asp:linkbutton>&nbsp;
            <asp:linkbutton id="btnLast" onclick="PagerButtonClick" runat="server"
     Font-Name="verdana" Font-size="8pt" ForeColor="navy" CommandArgument="last">
     </asp:linkbutton>
          </p>
        </form>
      </body>
    </HTML>Imports System.Data.SqlClient
    Imports System.Data
    Imports System.Web.UIPublic Class DataGridPaging
      Inherits System.Web.UI.Page  Protected WithEvents MyDataGrid As System.Web.UI.WebControls.DataGrid
      Protected WithEvents btnFirst As System.Web.UI.WebControls.LinkButton
      Protected WithEvents btnPrev As System.Web.UI.WebControls.LinkButton
      Protected WithEvents btnNext As System.Web.UI.WebControls.LinkButton
      Protected WithEvents btnLast As System.Web.UI.WebControls.LinkButton
      Protected WithEvents lblCurrentIndex As System.Web.UI.WebControls.Label
      Protected WithEvents lblPageCount As System.Web.UI.WebControls.Label
      Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid#Region " Web Form Designer Generated Code "  'This call is required by the Web Form Designer.
      <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()  End Sub  Private Sub Page_Init(ByVal sender As System.Object,_
       ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
      End Sub#End Region  Private Sub Page_Load(ByVal sender As System.Object,_
       ByVal e As System.EventArgs) Handles MyBase.Load
        btnFirst.Text = "最首页"
        btnPrev.Text = "前一页"
        btnNext.Text = "下一页"
        btnLast.Text = "最后页"
        OpenDatabase()
        BindGrid()
      End Sub
      Sub MyDataGrid_Page(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs)
        Dim startIndex As Integer
        startIndex = MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize
        MyDataGrid.CurrentPageIndex = e.NewPageIndex
        BindGrid()
        ShowStats()
      End Sub  Sub BindGrid()
        Dim myConnection As SqlConnection = cn
        Dim ds As DataSet = New DataSet()
        Dim adapter As SqlDataAdapter = New SqlDataAdapter("Select * from Orders", myConnection)
        adapter.Fill(ds, "Orders")
        MyDataGrid.DataSource = ds.Tables("Orders").DefaultView
        MyDataGrid.DataBind()
        ShowStats()  End Sub  Sub PagerButtonClick(ByVal sender As Object, ByVal e As EventArgs)
        'used by external paging UI
        Dim arg As String = sender.CommandArgument    Select Case arg
          Case "next"
            If (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1)) Then
              MyDataGrid.CurrentPageIndex += 1
            End If
          Case "prev"
            If (MyDataGrid.CurrentPageIndex > 0) Then
              MyDataGrid.CurrentPageIndex -= 1
            End If
          Case "last"
            MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1)
          Case Else
            'page number
            MyDataGrid.CurrentPageIndex = System.Convert.ToInt32(arg)
        End Select
        BindGrid()
        ShowStats()
      End Sub  Sub ShowStats()
        lblCurrentIndex.Text = "第 " + (MyDataGrid.CurrentPageIndex + 1).ToString() + " 页"
        lblPageCount.Text = "总共 " + MyDataGrid.PageCount.ToString() + " 页"
      End Sub Public cn As New SqlClient.SqlConnection()
      Public Sub OpenDatabase()
        cn.ConnectionString = "Server=.;Database=NorthWind;User Id=sa;Password=;"
        cn.Open()
      End Sub
    End Class
      

  7.   

    参考:
    private void dgPagerDel_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    int empID = (int)dgPagerDel.DataKeys[e.Item.ItemIndex];
    string sqlCom = "delete Employees where EmployeeID="+empID.ToString();
    //定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
    SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["DataBaseCon"].ToString());
    //定义命令对象
    SqlCommand cmd = new SqlCommand(sqlCom,conn);
    //打开数据连接
    conn.Open();
    try
    {
    //执行SQL命令
    cmd.ExecuteNonQuery();
    //取得当前页的索引
    lastEditedPage = dgPagerDel.CurrentPageIndex;
    //如果有多页并且当前页中的项数仅有一项
    if ((dgPagerDel.PageCount - dgPagerDel.CurrentPageIndex) == 1 && dgPagerDel.Items.Count == 1)
    {
    if (dgPagerDel.PageCount > 1)
    {
    lastEditedPage = lastEditedPage - 1;
    }
    else
    {
    lastEditedPage = 0;
    }
    }
    dgPagerDel.CurrentPageIndex = lastEditedPage;
    DataGridDataBind();
    }
    catch(SqlException err)
    {
    //输出异常信息
    Response.Write(err.ToString());
    }
    finally
    {
    //关闭连接对象
    conn.Close();
    }
    }
      

  8.   

    是啊 同意  houjianxun(三千溺水)(独取一瓢清泉) 的说法