Click here to Skip to main content
14,238,693 members

Why GridView RowUpdating Event is not giving the updated values?

Rate this:
4.93 (5 votes)
Please Sign up or sign in to vote.
4.93 (5 votes)
20 Jul 2014CPOL
This is a most common question in Forums. We will find the actual cause, for which the RowUpdating Event behaves abnormal.

Debugger Inside GridView RowUpdating Event

Debugger Inside GridView RowUpdating Event

This is a very common question in forums. We will find the actual cause, for which the RowUpdating Event behaves abnormal.

Walk Through

GridView on Browser Would Look Like…

GridView on Browser

GridView on Browser

Now, Let’s Test

To test the Edit and Update features, let’s click on Edit Button in any Row. We can see that GridView adds TextBoxes in all the cells of that Row immediately. It also shows you Update and Cancel Buttons where it was previously showing Edit Button.

Let’s do some editing in the cells and click Update Button.

GridView Cell Showing Updated Value

GridView Cell Showing Updated Value
  1. Add a GridView in aspx page and define all its required properties and Events.
    <asp:GridView ID="gvTestGrid" runat="server" AutoGenerateColumns="false" 
            <asp:BoundField DataField="Column1" HeaderText="Some Column"/>
            <asp:CommandField ShowEditButton="true" />
  2. Bind the GridView from back end. For our example, we will bind one DataTable.
    private void BindGrid()
        // Add Column for the DataTable.
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("Column1"));
        // Add Rows to DataTable.
        for (int i = 1; i < 5; i++)
    	    DataRow dr = dt.NewRow();
    	    dr["Column1"] = "Old Value" + i;
        gvTestGrid.DataSource = dt;
  3. Define the GridView Events for Edit and Update operation.
    protected void gvTestGrid_RowEditing(object sender, GridViewEditEventArgs e)
        //Set the edit index.
        gvTestGrid.EditIndex = e.NewEditIndex;
        //Bind data to the GridView control.
    protected void gvTestGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
        //Get the new Values.
        GridViewRow row = gvTestGrid.Rows[e.RowIndex];
        string column1Value = ((TextBox)row.Cells[0].Controls[0]).Text;
        // Code to update the DataSource.
        //Reset the edit index.
        gvTestGrid.EditIndex = -1;
        //Bind data to the GridView control.
    protected void gvTestGrid_RowCancelingEdit(object sender, 
                                               GridViewCancelEditEventArgs e)
        //Reset the edit index.
        gvTestGrid.EditIndex = -1;
        //Bind data to the GridView control.
  4. It hits the breakpoint, we have set inside the RowUpdating Event. If we move our mouse on to the variables, which hold the cell values, we can see the old value instead of new updated value (Refer to the debugging screenshot at the top). So, we are stuck now, this is the bug.

What To Do Now?

Don’t panic. We need to find out what exactly in code is helping RowUpdating Event to give old values. So, the most wild guess is that, something is causing the Grid to bind again before RowUpdating Event, as we initially did during load.

If you remember, in Step 2, we did bind the Grid, which is actually called on the Page Load like…

protected void Page_Load(object sender, EventArgs e)

According to the Event Life Cycle, when any Control Event is fired (here Grid RowUpdating), it first goes to Page Load. As it is coming to Page Load, so it calls the Bind method again and re-binds the Grid. Now, it goes to RowUpdating Event, but the updated values are lost because Grid is already reset in Page Load.

So, How to Resolve?

IsPostBack() is the answer. We have to check if the Page is posted back or not. If not, then we will bind the Grid.

protected void Page_Load(object sender, EventArgs e)
    if (!IsPostBack)

Go and Share This With Your Friends

This issue sometimes kills time. So, don’t wait, just share with everybody you know, who can understand this. Also provide your valuable feedback here. Thanks for reading.


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


About the Author

Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)
Software Developer Mindfire Solutions
India India
Official Facebook Page - Tadit Dash

Proud Indian | Microsoft MVP | CodeProject MVP | Speaker | Author | DZone Most Valuable Blogger | Community Expert | Moderator | jsfiddler | Cook | Dancer

Currently working @Mindfire Solutions.
Love to code and help guys to Trap the Bug.

My Tech Blog

Programming Community Profiles

jsfiddle | Stack Overflow

Other Blog Profiles

Mindfire CRM Team Blog | Mindfire Blog

Social Profiles

Facebook | Twitter | LinkedIn | Google+


  1. DZone Most Valuable Blogger
  2. Microsoft MVP 2014, 2015, 2016
  3. Code Project MVP 2014, 2015, 2016
  4. Star Achiever of the Month December 2013
  5. Mindfire Techno Idea Contest 2013 Winner
  6. Star of the Month July 2013
  7. </

Comments and Discussions

-- There are no messages in this forum --
Technical Blog
Posted 2 Jul 2014

Tagged as


5 bookmarked