This documentation is archived and is not being maintained.

GridView.RowDeleting Event

Updated: July 2009

Occurs when a row's Delete button is clicked, but before the GridView control deletes the row.

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

JScript does not support events.
<asp:GridView OnRowDeleting="GridViewDeleteEventHandler" />

The RowDeleting event is raised when a row's Delete button is clicked, but before the GridView control deletes the row. This enables you to provide an event-handling method that performs a custom routine, such as canceling the delete operation, whenever this event occurs.

A GridViewDeleteEventArgs object is passed to the event-handling method, which enables you to determine the index of the current row and to indicate that the delete operation should be canceled. To cancel the delete operation, set the Cancel property of the GridViewDeleteEventArgs object to true. You can also manipulate the Keys and Values collections, if necessary, before the values are passed to the data source.

For more information about handling events, see Consuming Events.

The following example shows how to use the RowDeleting event to cancel the delete operation. The page contains a GridView control that displays a list of customer names and addresses from the AdventureWorksLT database. When the user clicks the Delete link for a row, the handler for the RowDeleting event checks the last name of the person displayed in the row that the user is trying to delete. If the last name is "Beaver", the delete operation is canceled, and an error message is displayed. For any other name, the delete operation proceeds and the row is deleted.

The event handler uses the RowIndex property of the GridViewDeleteEventArgs object to find the row that the user is trying to delete. The example examines the contents of the Rows collection. If the value you want to compare to is a key value, you could examine the DataKeys collection instead.

Rows are deleted from the CustomerAddress table instead of the Customer table in order to keep the example simple. The GridView control shows the result of joining three tables: Customer, Address, and CustomerAddress. When a CustomerAddress row is deleted, the corresponding GridView row disappears. Referential integrity constraints would make the code for an example that actually deletes rows from the Customer table more complex.

No code example is currently available or this language may not be supported.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0




July 2009

Rewrote example to make it clearer.

Customer feedback.