GridView.EditIndex Property

Gets or sets the index of the row to edit.

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

public virtual int EditIndex { get; set; }
/** @property */
public int get_EditIndex ()

/** @property */
public void set_EditIndex (int value)

public function get EditIndex () : int

public function set EditIndex (value : int)

Not applicable.

Property Value

The zero-based index of the row to edit. The default is -1, which indicates that no row is being edited.

Exception typeCondition


The specified index is less than -1.

Use the EditIndex property to programmatically specify or determine which row in a GridView control to edit. When this property is set to the index of a row in the control, that row enters edit mode. In edit mode, each column field in the row that is not read-only displays the appropriate input control for the field's data type, such as a TextBox control. This allows the user to modify the field's value. To exit edit mode, set this property to -1.


The GridView control has a built-in editing feature that automatically sets this property. This property is typically used only when you need to programmatically determine which row is being edited or when you are adding your own custom editing functionality to the GridView control.

The following code example demonstrates how to use the EditIndex property to determine which row is being edited in a GridView control. After a row is updated, a message is displayed to indicate that the update was successful.

<%@ Page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">
  void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
    // Clear the message label when the user enters edit mode.
    if (e.CommandName == "Edit")
      Message.Text = "";

  void CustomersGridView_RowUpdated(Object sender, GridViewUpdatedEventArgs e)
        // The update operation was successful. Retrieve the row being edited.
        int index = CustomersGridView.EditIndex;
        GridViewRow row = CustomersGridView.Rows[index];
        // Notify the user that the update was successful.
        Message.Text = "Updated record " + row.Cells[1].Text + ".";

  void CustomersGridView_RowCancelingEdit(Object sender, GridViewCancelEditEventArgs e)
        // The update operation was canceled. Display the appropriate message.
        Message.Text = "Update operation canceled.";


<html xmlns="" >
  <head runat="server">
    <title>GridView Rows Example</title>
    <form id="form1" runat="server">
      <h3>GridView Rows Example</h3>
      <asp:label id="Message"
      <!-- The GridView control automatically sets the columns     -->
      <!-- specified in the datakeynames property as read-only.    -->
      <!-- No input controls are rendered for these columns in     -->
      <!-- edit mode.                                              -->
      <asp:gridview id="CustomersGridView"
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
        deletecommand="Delete from Customers where CustomerID = @CustomerID"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"

Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions