This documentation is archived and is not being maintained.

GridView.RowUpdating Event

Updated: August 2008

Occurs when a row's Update button is clicked, but before the GridView control updates the row.

Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)

public event GridViewUpdateEventHandler RowUpdating
<asp:GridView OnRowUpdating="GridViewUpdateEventHandler" />

The RowUpdating event is raised when a row's Update button is clicked, but before the GridView control updates the row. This enables you to provide an event-handling method that performs a custom routine, such as canceling the update operation, whenever this event occurs.

A GridViewUpdateEventArgs object is passed to the event-handling method, which enables you to determine the index of the current row and to indicate that the update operation should be canceled. To cancel the update operation, set the Cancel property of the GridViewUpdateEventArgs object to true. You can also manipulate the Keys, OldValues, and NewValues collections, if necessary, before the values are passed to the data source. A common way to use these collections is to HTML-encode the values supplied by the user before they are stored in the data source. This helps to prevent script injection attacks.


The Keys, OldValues and NewValues collections are automatically populated only when the GridView control is bound to data by using the DataSourceID property.

For more information about handling events, see Consuming Events.

The following example demonstrates how to use the RowUpdating event to update the values in the data source object when the data source is set programmatically.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<script runat="server">

  protected void Page_Load(object sender, EventArgs e)

    if (!Page.IsPostBack)
      // Create a new table.
      DataTable taskTable = new DataTable("TaskList");

      // Create the columns.
      taskTable.Columns.Add("Id", typeof(int));
      taskTable.Columns.Add("Description", typeof(string));
      taskTable.Columns.Add("IsComplete", typeof(bool) );

      //Add data to the new table. 
      for (int i = 0; i < 20; i++)
        DataRow tableRow = taskTable.NewRow();
        tableRow["Id"] = i;
        tableRow["Description"] = "Task " + i.ToString();
        tableRow["IsComplete"] = false;            

      //Persist the table in the Session object.
      Session["TaskTable"] = taskTable;

      //Bind data to the GridView control.


  protected void TaskGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    TaskGridView.PageIndex = e.NewPageIndex;
    //Bind data to the GridView control.

  protected void TaskGridView_RowEditing(object sender, GridViewEditEventArgs e)
    //Set the edit index.
    TaskGridView.EditIndex = e.NewEditIndex;
    //Bind data to the GridView control.

  protected void TaskGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    //Reset the edit index.
    TaskGridView.EditIndex = -1;
    //Bind data to the GridView control.

  protected void TaskGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    //Retrieve the table from the session object.
    DataTable dt = (DataTable)Session["TaskTable"];

    //Update the values.
    GridViewRow row = TaskGridView.Rows[e.RowIndex];
    dt.Rows[row.DataItemIndex]["Id"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
    dt.Rows[row.DataItemIndex]["Description"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
    dt.Rows[row.DataItemIndex]["IsComplete"] = ((CheckBox)(row.Cells[3].Controls[0])).Checked;

    //Reset the edit index.
    TaskGridView.EditIndex = -1;

    //Bind data to the GridView control.

  private void BindData()
    TaskGridView.DataSource = Session["TaskTable"];

<html xmlns="">
<head runat="server">
    <title>GridView example</title>
    <form id="form1" runat="server">

      <asp:GridView ID="TaskGridView" runat="server" 


Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0




August 2008

Added a note to the Remarks section and made changes to the second example.

Customer feedback.