GridView.RowDeleting (Evento)
Se produce cuando se hace clic en el botón Eliminar de una fila, pero antes de que el control GridView elimine la fila.

Espacio de nombres: System.Web.UI.WebControls
Ensamblado: System.Web (en system.web.dll)

Sintaxis

Visual Basic (Declaración)
Public Event RowDeleting As GridViewDeleteEventHandler
Visual Basic (Uso)
Dim instance As GridView
Dim handler As GridViewDeleteEventHandler

AddHandler instance.RowDeleting, handler
C#
public event GridViewDeleteEventHandler RowDeleting
C++
public:
event GridViewDeleteEventHandler^ RowDeleting {
    void add (GridViewDeleteEventHandler^ value);
    void remove (GridViewDeleteEventHandler^ value);
}
J#
/** @event */
public void add_RowDeleting (GridViewDeleteEventHandler value)

/** @event */
public void remove_RowDeleting (GridViewDeleteEventHandler value)
JScript
En JScript, se pueden controlar los eventos que define una clase, pero no se pueden definir unos propios.
XAML
No aplicable.
Comentarios

El evento RowDeleting se produce cuando se hace clic en el botón Eliminar de una fila, pero antes de que el control GridView elimine la fila. Esto permite proporcionar un método de control de eventos que realice una rutina personalizada, como cancelar la operación de eliminación, siempre que se produzca este evento.

Se pasa un objeto GridViewDeleteEventArgs al método de control de eventos que permite determinar el índice de la fila actual e indicar que se debe cancelar la operación de eliminación. Para cancelar la operación de eliminación, establezca la propiedad Cancel del objeto GridViewDeleteEventArgs en true. También puede manipular las colecciones Keys y Values, si es necesario, antes de pasar valores al origen de datos.

Para obtener más información sobre la forma de controlar eventos, vea Utilizar eventos.

Ejemplo

En el ejemplo de código siguiente se muestra cómo utilizar el evento RowDeleting para cancelar la operación de eliminación si el usuario intenta quitar el último registro de un control GridView.

Visual Basic
<%@ Page language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  Sub CustomersGridView_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
      
    ' Cancel the delete operation if the user attempts to remove
    ' the last record from the GridView control.
    If CustomersGridView.Rows.Count <= 1 Then
        
      e.Cancel = True
      Message.Text = "You must keep at least one record."
            
    End If
    
  End Sub

</script>

<html  >
  <head runat="server">
    <title>GridView RowDeleting Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridView RowDeleting 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" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="true"
        autogeneratedeletebutton="true"
        datakeynames="CustomerID"
        onrowdeleting="CustomersGridView_RowDeleting"  
        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]"
        deletecommand="Delete from Customers where CustomerID = @CustomerID"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>
      
    </form>
  </body>
</html>

C#
<%@ 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_RowDeleting(Object sender, GridViewDeleteEventArgs e)
  {
    
    // Cancel the delete operation if the user attempts to remove
    // the last record from the GridView control.
    if (CustomersGridView.Rows.Count <= 1)
    {
        
      e.Cancel = true;
      Message.Text = "You must keep at least one record.";
            
    }
    
  }  

</script>

<html  >
  <head runat="server">
    <title>GridView RowDeleting Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridView RowDeleting 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" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="true"
        autogeneratedeletebutton="true"
        datakeynames="CustomerID"
        onrowdeleting="CustomersGridView_RowDeleting"  
        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]"
        deletecommand="Delete from Customers where CustomerID = @CustomerID"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>
      
    </form>
  </body>
</html>

Plataformas

Windows 98, Windows 2000 Service Pack 4, Windows CE, Windows Millennium, Windows Mobile para Pocket PC, Windows Mobile para Smartphone, Windows Server 2003, Windows XP Media Center, Windows XP Professional x64, Windows XP SP2, Windows XP Starter

Microsoft .NET Framework 3.0 es compatible con Windows Vista, Microsoft Windows XP SP2 y Windows Server 2003 SP1.

Información de versión

.NET Framework

Compatible con: 3.0, 2.0
Vea también

Page view tracker