Click here to Skip to main content
15,885,792 members
Articles / Gridview

Why GridView RowUpdating Event is not giving the updated values?

20 Jul 2014CPOL2 min read 37.8K   5  
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.
    XML
    <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.
    C#
    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 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…

C#
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.

C#
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.

Image 4 Image 5

License

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


Proud Indian | Author | TEDx Speaker | Microsoft MVP | CodeProject MVP | Speaker | DZone Most Valuable Blogger| jsfiddler

My Website

taditdash.com

Programming Community Profiles

jsfiddle | Stack Overflow

Social Profiles

Facebook | Twitter | LinkedIn

Awards


  1. DZone Most Valuable Blogger
  2. Microsoft MVP 2014, 2015, 2016, 2017, 2018
  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

Comments and Discussions

 
-- There are no messages in this forum --