按下更新没有保存数据并恢复为旧数据

时间:2014-01-25 10:30:44

标签: asp.net vb.net gridview

我遇到“Sub GridView1_OnRowUpdating”问题。

按下编辑状态时,单元格中的数据会发生变化,按下更新后数据将恢复为原始数据,不会存储数据。

<asp:GridView ID="GridView1" runat="server" AllowSorting="true" AutoPostBack="True" AutoGenerateColumns="True" AutoGenerateEditButton="True" OnRowCancelingEdit="Gridview1_OnRowCancelingEdit" OnRowEditing="Gridview1_OnRowEditing" OnRowUpdating="GridView1_OnRowUpdating" 
    AllowPaging="True" PageSize="50" OnPageIndexChanging="GridView1_PageIndexChanging">
</asp:GridView>



Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        ViewState("pageIndex") = 1
        ViewState("edit") = -1
        ShowGrid()
    End If
End Sub
Sub ShowGrid()
    Dim connStr, cmdStr As String
    connStr = "connection string works"
    cmdStr = "SELECT * FROM OrbitDates;"
    Dim ds As New DataSet
    Dim dt As New DataTable()
    Try
        Using conn As New SqlConnection(connStr)
            Using cmd As New SqlCommand(cmdStr, conn)
                conn.Open()
                cmd.ExecuteNonQuery()
                Using da As New SqlDataAdapter(cmd)
                    da.Fill(ds)
                    dt = ds.Tables(0)
                    GridView1.EditIndex = Convert.ToInt32(ViewState("edit"))
                    GridView1.DataSource = dt
                    GridView1.DataBind()
                    GridView1.PageIndex = Convert.ToInt32(ViewState("pageIndex"))
                End Using
                conn.Close()
                cmd.Dispose()
                conn.Dispose()
            End Using
        End Using
    Catch ex As Exception

    End Try
End Sub
Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs) Handles GridView1.PageIndexChanging
    GridView1.PageIndex = e.NewPageIndex
    ViewState("pageIndex") = e.NewPageIndex.ToString()
    ShowGrid()
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    ViewState("pageIndex") = 1
    ShowGrid()
End Sub
Protected Sub GridView1_OnRowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs) Handles GridView1.RowEditing
    GridView1.EditIndex = e.NewEditIndex
    ViewState("edit") = GridView1.EditIndex
    ShowGrid()
End Sub
Protected Sub GridView1_OnRowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
    GridView1.EditIndex = -1
    ViewState("edit") = GridView1.EditIndex
    ShowGrid()
End Sub
Protected Sub GridView1_OnRowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) Handles GridView1.RowUpdating
    Dim connStr, cmdStr As String
    connStr = "connection string works"
    cmdStr = "UPDATE OrbitDates SET JD=@JD,Xecl1=@Xecl1,Yecl1=@Yecl1,Zecl1=@Zecl1 WHERE ido=@ido;"
    Try
        Using conn As New SqlConnection(connStr)
            Using cmd As New SqlCommand(cmdStr, conn)
                conn.Open()
                cmd.Parameters.AddWithValue("@ido", GridView1.Rows(e.RowIndex).Cells(0).Text)
                cmd.Parameters.AddWithValue("@JD", GridView1.Rows(e.RowIndex).Cells(1).Text)
                cmd.Parameters.AddWithValue("@Xecl1", GridView1.Rows(e.RowIndex).Cells(2).Text)
                cmd.Parameters.AddWithValue("@Yecl1", GridView1.Rows(e.RowIndex).Cells(3).Text)
                cmd.Parameters.AddWithValue("@Zecl1", GridView1.Rows(e.RowIndex).Cells(4).Text)
                cmd.ExecuteNonQuery()
                conn.Close()
                cmd.Dispose()
                conn.Dispose()
            End Using
        End Using
    Catch ex As Exception
        Throw ex
    End Try
    ViewState("edit") = e.RowIndex
    ShowGrid()
End Sub

0 个答案:

没有答案