Joe Yan
Joe Yan

Reputation: 2095

Gridview - Cannot get updated cell value for RowUpdating

I am using ItemTemplate & EditTemplate for editing the gridview. (ASP.net + VB).

I click EDIT button then I can check/uncheck those checkbox and amend the textbox value. When click UPDATE button, it will fire the RowUpdating Event, But I found that when I get the value for update statement, it still get the value before editing, not updated value.

How can I get the latest & updated value ? Thanks. Joe

The following is the VB code:

Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)

    'Update the values.
    Dim row = Gridview1.Rows(e.RowIndex)

    Dim Col1_SL = CType(Gridview1.Rows(e.RowIndex).FindControl("cb1_SL"), CheckBox)
    Dim Col1_VL = CType(Gridview1.Rows(e.RowIndex).FindControl("cb1_VL"), CheckBox)
    Dim Col1_ML = CType(Gridview1.Rows(e.RowIndex).FindControl("cb1_ML"), CheckBox)
    Dim Col1_PH = CType(Gridview1.Rows(e.RowIndex).FindControl("cb1_PH"), CheckBox)
    Dim Col1_APH = CType(Gridview1.Rows(e.RowIndex).FindControl("cb1_APH"), CheckBox)
    Dim Col1_TOIL = CType(Gridview1.Rows(e.RowIndex).FindControl("cb1_TOIL"), CheckBox)
    Dim Col1_Others = CType(Gridview1.Rows(e.RowIndex).FindControl("tb1_Others"), TextBox)
    Dim Col1_RosterKey = CType(Gridview1.Rows(e.RowIndex).FindControl("lb1_rosterkey"), Label)

    Using conn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("hris_shiftdutyConnectionString").ConnectionString)
        conn.Open()
        cmd.Connection = conn
        sql = "SET DATEFORMAT dmy;UPDATE troster SET SL='" & Convert.ToInt32(Col1_SL.Checked) & "' where roster_key='" & Col1_RosterKey.Text & "';"
        cmd.CommandText = Sql
        reader = cmd.ExecuteReader()
        conn.Close()
        reader.Close()
    End Using

    'Reset the edit index.
    Gridview1.EditIndex = -1

    'Bind data to the GridView control.
    BindData()
End Sub

Upvotes: 0

Views: 5013

Answers (2)

codeandcloud
codeandcloud

Reputation: 55200

The most likely reason will be this.
You are calling BindData() on Page_Load without using !IsPostBack

Protected Sub Page_Load Handles Me.Load
    If Not IsPostBack
        ' Bind Grid only at the first load
        ' Do not load Grid again at Postbacks
        BindData()
    End If
End Sub

Upvotes: 1

user815091
user815091

Reputation: 1

You have 2 options:

  1. There is a separate RowUpdated method that is fired after updates have been performed by the data source that is bound to the grid view. This method will contain the new values for edits and inserts.
  2. The RowUpdating method should have a parameter of type GridViewEventArgs. This will have a property called NewValues that contains the new values. In your example this was the variable e.

Upvotes: 0

Related Questions