Gridview编辑更新到MS SQL数据库

时间:2014-01-23 17:31:04

标签: asp.net vb.net gridview

我遇到OnRowUpdating事件时遇到问题。它不会将新数据保存在GridView1.EditIndex行中。只要按下更新,它就会恢复为旧值。

OnRowEditing似乎运作正常。

同样适用于OnRowCancelingEdit它似乎正常运作。

ASPX:

<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
    ViewState("pageIndex") = 1
    ViewState("edit") = -1
    ShowGrid()
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"))
                    dt.DefaultView.Sort = ViewState("Sort")
                    GridView1.DataSource = dt.DefaultView
                    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 GridView1_OnRowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
    GridView1.EditIndex = e.NewEditIndex
    ViewState("edit") = GridView1.EditIndex
    ShowGrid()
End Sub
Protected Sub GridView1_OnRowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
    GridView1.EditIndex = -1
    ViewState("edit") = GridView1.EditIndex
    ShowGrid()
End Sub
Protected Sub GridView1_OnRowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
    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

2 个答案:

答案 0 :(得分:1)

问题是在执行GridView1_OnRowUpdating之前,GridView被旧数据覆盖,因为这段代码:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    ViewState("pageIndex") = 1
    ViewState("edit") = -1
    ShowGrid()
End Sub

您需要将If Not IsPostBack放入Page_Load

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

尝试详细了解ASP.NET Page Life Cycle

答案 1 :(得分:0)

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    ViewState("pageIndex") = 1
    ViewState("edit") = -1
    ShowGrid()
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"))
                    dt.DefaultView.Sort = ViewState("Sort")
                    GridView1.DataSource = dt.DefaultView
                    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 GridView1_OnRowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
    GridView1.EditIndex = e.NewEditIndex
    ViewState("edit") = GridView1.EditIndex
    ShowGrid()
End Sub
Protected Sub GridView1_OnRowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
    GridView1.EditIndex = -1
    ViewState("edit") = GridView1.EditIndex
    ShowGrid()
End Sub
Protected Sub GridView1_OnRowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
    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