Export (0) Print
Expand All
0 out of 3 rated this helpful - Rate this topic

GridView.RowUpdating Event

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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<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="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>GridView example</title>
    <form id="form1" runat="server">

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


.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft. All rights reserved.