System.Web.UI.WebControls.GridView.RowCommand Event

Occurs when a button is clicked in a System.Web.UI.WebControls.GridView control.


public event GridViewCommandEventHandler RowCommand


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

Buttons within a System.Web.UI.WebControls.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.


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


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


Puts the current record in edit mode. Raises the GridView.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 GridView.PageIndexChanging and GridView.PageIndexChanged events.


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


Sorts the System.Web.UI.WebControls.GridView control. Raises the GridView.Sorting and GridView.Sorted events.


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

Although the GridView.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 System.Web.UI.WebControls.GridViewCommandEventArgs object is passed to the event-handling method, which enables you to determine the command name and command argument of the button clicked.


To determine the index of the row that raised the event, use the CommandEventArgs.CommandArgument property of the event argument that is passed to the event. The System.Web.UI.WebControls.ButtonField class automatically populates the CommandEventArgs.CommandArgument property with the appropriate index value. For other command buttons, you must manually set the Button.CommandArgument property of the command button. For example, you can set the Button.CommandArgument to <%# Container.DataItemIndex %> when the System.Web.UI.WebControls.GridView control has no paging enabled.

For more information about handling events, see Consuming Events.


Namespace: System.Web.UI.WebControls
Assembly: System.Web (in System.Web.dll)
Assembly Versions:
Since: .NET 2.0