GridView.RowDeleting Event


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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)

public event GridViewDeleteEventHandler RowDeleting

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 NIB: Consuming Events.

The following example demonstrates 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.

For information about how to set up the AdventureWorksLT database, see How to: Set Up an AdventureWorksLT Sample Database for ASP.NET Development.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<script runat="server">

    void CustomersGridView_RowDeleting
        (Object sender, GridViewDeleteEventArgs e)
        TableCell cell = CustomersGridView.Rows[e.RowIndex].Cells[2];
        if (cell.Text == "Beaver")
            e.Cancel = true;
            Message.Text = "You cannot delete customer Beaver.";
            Message.Text = "";


<html xmlns="">
<head id="Head1" runat="server">
    <title>GridView RowDeleting Example</title>
    <form id="form1" runat="server">
        GridView RowDeleting Example
    <asp:Label ID="Message" ForeColor="Red" runat="server" />
    <br />
    <asp:GridView ID="CustomersGridView" runat="server" 
            <asp:BoundField DataField="FirstName" 
                HeaderText="FirstName" SortExpression="FirstName" />
            <asp:BoundField DataField="LastName" HeaderText="LastName" 
                SortExpression="LastName" />
            <asp:BoundField DataField="City" HeaderText="City" 
                SortExpression="City" />
            <asp:BoundField DataField="StateProvince" HeaderText="State" 
                SortExpression="StateProvince" />
    <asp:SqlDataSource ID="CustomersSqlDataSource" runat="server"
        SelectCommand="SELECT SalesLT.CustomerAddress.CustomerID, 
            FROM SalesLT.Customer 
            INNER JOIN SalesLT.CustomerAddress 
            ON SalesLT.Customer.CustomerID = 
            INNER JOIN SalesLT.Address ON SalesLT.CustomerAddress.AddressID = 
        DeleteCommand="Delete from SalesLT.CustomerAddress where CustomerID = 
            @CustomerID and AddressID = @AddressID" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorksLTConnectionString %>">
            <asp:Parameter Name="AddressID" />
            <asp:Parameter Name="CustomerID" />

.NET Framework
Available since 2.0
Return to top