GridView.RowCommand Event

Occurs when a button is clicked in a GridView control.

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

public event GridViewCommandEventHandler RowCommand
/** @event */
public void add_RowCommand (GridViewCommandEventHandler value)

/** @event */
public void remove_RowCommand (GridViewCommandEventHandler value)

In JScript, you can handle the events defined by a class, but you cannot define your own.
Not applicable.

The RowCommand event is raised when a button is clicked in the GridView control. This allows you to provide an event-handling method that performs a custom routine whenever this event occurs.

Buttons within a GridView control can also invoke some of the built-in functionality of the control. To perform one of these operations, set the CommandName property of a button to one of the values in the following table.

CommandName value



Cancels an edit operation and returns the GridView control to read-only mode. Raises the RowCancelingEdit event.


Deletes the current record. Raises the RowDeleting and RowDeleted events.


Puts the current record in edit mode. Raises the RowEditing event.


Performs a paging operation. Sets the CommandArgument property of the button to "First", "Last", "Next", "Prev", or a page number to specify the type of paging operation to perform. Raises the PageIndexChanging and PageIndexChanged events.


Selects the current record. Raises the SelectedIndexChanging and SelectedIndexChanged events.


Sorts the GridView control. Raises the Sorting and Sorted events.


Updates the current record in the data source. Raises the RowUpdating and RowUpdated events.

Although the RowCommand event is raised when a button listed in the previous table is clicked, it is recommended that you use the events listed in the table for the operation.

A GridViewCommandEventArgs object is passed to the event-handling method, which allows you to determine the command name and command argument of the button clicked.


The GridViewCommandEventArgs class does not contain a property to indicate which row's button was clicked. If you need to know which row raised the event, pass the row's index to the event-handling method using the CommandArgument property.

For more information about handling events, see Consuming Events.

The following code example demonstrates how to use the RowCommand event to add the name of a customer from a GridView control to a ListBox control when a row's Add button is clicked.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">

  void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
    // If multiple buttons are used in a GridView control, use the
    // CommandName property to determine which button was clicked.
      // Convert the row index stored in the CommandArgument
      // property to an Integer.
      int index = Convert.ToInt32(e.CommandArgument);
      // Retrieve the row that contains the button clicked 
      // by the user from the Rows collection.
      GridViewRow row = CustomersGridView.Rows[index];
      // Create a new ListItem object for the customer in the row.     
      ListItem item = new ListItem();
      item.Text = Server.HtmlDecode(row.Cells[2].Text);
      // If the customer is not already in the ListBox, add the ListItem 
      // object to the Items collection of the ListBox control. 
      if (!CustomersListBox.Items.Contains(item))

  void CustomersGridView_RowCreated(Object sender, GridViewRowEventArgs e)
    // The GridViewCommandEventArgs class does not contain a 
    // property that indicates which row's command button was
    // clicked. To identify which row's button was clicked, use 
    // the button's CommandArgument property by setting it to the 
    // row's index.
    if(e.Row.RowType == DataControlRowType.DataRow)
      // Retrieve the LinkButton control from the first column.
      LinkButton addButton = (LinkButton)e.Row.Cells[0].Controls[0];
      // Set the LinkButton's CommandArgument property with the
      // row's index.
      addButton.CommandArgument = e.Row.RowIndex.ToString();


<html xmlns="" >
  <head runat="server">
    <title>GridView RowCommand Example</title>
    <form id="form1" runat="server">
      <h3>GridView RowCommand Example</h3>
      <table width="100%">         
          <td style="width:50%">
            <asp:gridview id="CustomersGridView" 
                <asp:buttonfield buttontype="Link" 
                <asp:boundfield datafield="CustomerID" 
                  headertext="Customer ID"/>
                <asp:boundfield datafield="CompanyName" 
                  headertext="Company Name"/> 
                <asp:boundfield datafield="City" 
          <td style="vertical-align:top; width:50%">
            Customers: <br/>
            <asp:listbox id="CustomersListBox"
      <!-- 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="CustomersSource"
        selectcommand="Select [CustomerID], [CompanyName], [City] From [Customers]"
        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