datagrid自定义分页功能.点击第二页正常.编辑的时候就跑到第一页了.不知道怎么解决..
代码:
Dim cn As New SqlConnection("server=.;database=testDb;uid=sa")
Dim rowCount As Integer
Dim startIndex As Integer
 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not IsPostBack Then
            DataGridDataBind()
        End If
    End Sub
 Private Sub DataGridDataBind()
        Try
          
            Dim da As New SqlDataAdapter("select bookName,UnitsInStock,UnitPrice,bookID,book.publishID,bookImg,book.CategoryID,bookBrief,auther,CategoryName,publishName from book,Categories,publish where book.CategoryID=Categories.CategoryID and book.publishID=publish.publishID", cn)
            Dim ds As New DataSet
            cn.Open()
            da.Fill(ds, startIndex, DataGrid1.PageSize, "book") '指定索引开始取记录
            da.Fill(ds, "AllDataTable")
            DataGrid1.VirtualItemCount = ds.Tables("AllDataTable").Rows.Count ' 获取自定义实际页数
            DataGrid1.DataSource = ds.Tables("book")
            rowCount = ds.Tables("book").Rows.Count
            DataGrid1.DataBind()
        Catch ex As Exception
            Response.Write(ex.Message)
        Finally
            cn.Close()
        End Try
    End Sub
 Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.EditCommand
        '编辑          DataGrid1.EditItemIndex = e.Item.ItemIndex
        Label1.Text = DataGrid1.CurrentPageIndex
        btnSelect.Enabled = False
        DataGridDataBind()
    End Sub
  Private Sub DataGrid1_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.CancelCommand
        '取消
        DataGrid1.EditItemIndex = -1
        DataGridDataBind()
        btnSelect.Enabled = True
    End Sub
 Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged
        DataGrid1.CurrentPageIndex = e.NewPageIndex
        startIndex = DataGrid1.PageSize * DataGrid1.CurrentPageIndex 
        DataGridDataBind()
    End Sub

解决方案 »

  1.   

    Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.EditCommand
    '编辑DataGrid1.EditItemIndex = e.Item.ItemIndex
    Label1.Text = DataGrid1.CurrentPageIndex
    btnSelect.Enabled = False
    DataGridDataBind()
    End Sub
    ============
    1。编辑事件处理程序调用 DataGridDataBind() 的时候,startIndex 并未正确赋值,应该是你的初始值 0, 导致 da.Fill(ds, startIndex, DataGrid1.PageSize, "book") 从第一页开始加载数据》》》btnSelect.Enabled = False
    startIndex = DataGrid1.PageSize * DataGrid1.CurrentPageIndex
    DataGridDataBind()