Export (0) Print
Expand All
Expand Minimize

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; }
<asp:GridView EditIndex="Int32" />

Property Value

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

ExceptionCondition
ArgumentOutOfRangeException

The specified index is less than -1.

The row index is zero-based (the first row is row zero).

This property is typically used only in the following scenarios, which involve handlers for specific events:

  • You want the GridView control to open in edit mode for a specific row the first time that the page is displayed. To do this, you can set the EditIndex property in the handler for the Load event of the Page class or of the GridView control.

  • You want to know which row was edited after the row was updated. To do this, you can retrieve the row index from the EditIndex property in the RowUpdated event handler.

  • You are binding the GridView control to a data source by setting the DataSource property programmatically. In this case you must set the EditIndex property in the RowEditing and RowCancelingEdit event handlers.

If you set the EditIndex property after a postback or in handlers for events that are raised later than the Load event, the GridView control might not enter edit mode for the specified row. If you read the value of this property in other event handlers, the index is not guaranteed to reflect the row that is being edited.

To determine which row the user has clicked an Edit button or hyperlink in before the GridView control enters edit mode, you can retrieve the row index from the NewEditIndex property of the GridViewEditEventArgs object in the RowEditing event handler.

To prevent the GridView control from entering edit mode after a user has clicked an Edit button or hyperlink, set the Cancel property of the GridViewEditEventArgs object to true in the RowEditing event handler.

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


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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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.";

    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView Rows Example</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>GridView Rows Example</h3>

      <asp:label id="Message"
        forecolor="Red"
        runat="server"/>

      <br/>    

      <!-- 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"
        allowpaging="true" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="true"
        autogenerateeditbutton="true"
        datakeynames="CustomerID"
        onrowcommand="CustomersGridView_RowCommand"
        onrowupdated="CustomersGridView_RowUpdated"
        onrowcancelingedit="CustomersGridView_RowCancelingEdit"  
        runat="server">
      </asp:gridview>

      <!-- 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%>"
        runat="server">
      </asp:sqldatasource>

    </form>
  </body>
</html>

.NET Framework

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

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.

Show:
© 2014 Microsoft