Click here to Skip to main content
Click here to Skip to main content

Tagged as

Why GridView RowUpdating Event is not giving the updated values?

, 20 Jul 2014
Rate this:
Please Sign up or sign in to vote.
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 most 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" 
                  OnRowCancelingEdit="gvTestGrid_RowCancelingEdit"
                  OnRowEditing="gvTestGrid_RowEditing" 
                  OnRowUpdating="gvTestGrid_RowUpdating">
        <Columns>
            <asp:BoundField DataField="Column1" HeaderText="Some Column"/>
            <asp:CommandField ShowEditButton="true" />
        </Columns>
    </asp:GridView>
    
  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;
    	    dt.Rows.Add(dr);
        }
    
        gvTestGrid.DataSource = dt;
        gvTestGrid.DataBind();
    }
  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.
        BindGrid();
    }
    
    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.
        BindGrid();
    }
    
    protected void gvTestGrid_RowCancelingEdit(object sender, 
                                               GridViewCancelEditEventArgs e)
    {
        //Reset the edit index.
        gvTestGrid.EditIndex = -1;
    
        //Bind data to the GridView control.
        BindGrid();
    }
    

  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 the debugging Screen Shot 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)
    {
        BindGrid();
    }
    

    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)
        {
            BindGrid();
        }
    }
    

    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.


License

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

Share

About the Author

Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)
Software Developer Mindfire Solutions
India India
Proud Indian | Microsoft MVP | CodeProject MVP | Author | Blogger | Community Expert | Moderator | jsfiddler | Cricketer | Cook | Dancer
 
Currently working @Mindfire Solutions.
Love to code and help guys to Trap the Bug.

My Tech Blog

taditdash.wordpress.com

Programming Community Profiles

jsfiddle | Stack Overflow

Other Blog Profiles

Mindfire CRM Team Blog | Mindfire Blog

Social Profiles

Facebook | Twitter | LinkedIn | Google+

Awards

  1. Microsoft MVP 2014
  2. Code Project MVP 2014
  3. Star Achiever of the Month December 2013
  4. Mindfire Techno Idea Contest 2013 Winner
  5. Star of the Month July 2013
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 21 Jul 2014
Article Copyright 2014 by Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid