Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
RowUpdating is not functioning. The edit works. You can write into the highlighted row but when the update button is pressed it just reverts back to the original values in the database.

The datakeys may shed some light into this mystery but I do not know what they are or how to use them.

Sub ShowGrid() populates the gvedit Gridview from the database and functions correctly. Sample:
VB
gvedit.EditIndex = Convert.ToInt32(ViewState("edit"))
gvedit.DataSource = dt
gvedit.DataBind()


VB
Protected Sub gvedit_OnRowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) Handles gvedit.RowUpdating
    Dim connStr, cmdStr As String
    connStr = "connection string works"
    cmdStr = "UPDATE test SET [datetime]=@datetime,[col1]=@col1,[col2]=@col2,[col3]=@col3 WHERE [idt]=@idt;"
    Try
        Using conn As New SqlConnection(connStr)
            Using cmd As New SqlCommand(cmdStr, conn)
                conn.Open()
                cmd.Parameters.AddWithValue("@idt", GridView1.Rows(e.RowIndex).Cells(0).Text)
                cmd.Parameters.AddWithValue("@datetime", DateTime.Now)
                cmd.Parameters.AddWithValue("@col1", GridView1.Rows(e.RowIndex).Cells(2).Text)
                cmd.Parameters.AddWithValue("@col2", GridView1.Rows(e.RowIndex).Cells(3).Text)
                cmd.Parameters.AddWithValue("@col3", GridView1.Rows(e.RowIndex).Cells(4).Text)
                cmd.ExecuteNonQuery()
                conn.Close()
                cmd.Dispose()
                conn.Dispose()
            End Using
        End Using
    Catch ex As Exception
        TextBox1.Text = ex.Message
    End Try
    ViewState("edit") = e.RowIndex
    ShowGrid()
End Sub


XML
<asp:GridView ID="gvedit" runat="server"  AutoGenerateColumns="True" AutoGenerateEditButton="True" OnRowCancelingEdit="gvedit_OnRowCancelingEdit" OnRowEditing="gvedit_OnRowEditing" OnRowUpdating="gvedit_OnRowUpdating"
    <asp:GridView>
Posted
Comments
Can you show the Page Load code?
teledexterus 27-Jan-14 5:25am    
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
Refer - GridView.RowUpdating Event[^].

It directly sets EditIndex inside the Event like...

TaskGridView.EditIndex = -1;

Why are you using ViewState for that? Try as suggested in the article.

Again... Have you checked the DataBase after the update whether it is reflecting the changes or not?
teledexterus 27-Jan-14 5:49am    
That is not the problem here it is the RowUpdating is not engaging!!!
Did you debug?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900