GridView.DeleteRow(Int32) Método

Definición

Elimina el registro en el índice especificado del origen de datos.

public:
 virtual void DeleteRow(int rowIndex);
public virtual void DeleteRow (int rowIndex);
abstract member DeleteRow : int -> unit
override this.DeleteRow : int -> unit
Public Overridable Sub DeleteRow (rowIndex As Integer)

Parámetros

rowIndex
Int32

Índice de la fila que se va a eliminar.

Excepciones

El control GridView no está enlazado a un control de origen de datos.

El control de origen de datos al que se enlaza el control GridView, no admite operaciones de eliminación, o no hay ningún comando de eliminación definido para el origen de datos.

Ejemplos

En el ejemplo siguiente se muestra cómo usar el DeleteRow método para eliminar mediante programación un registro en un GridView control .


<%@ 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 DeleteRowButton_Click(Object sender, EventArgs e)
  {
    // Programmatically delete the selected record.
    CustomersGridView.DeleteRow(CustomersGridView.SelectedIndex);
  }
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView DeleteRow Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridView DeleteRow Example</h3>

      <asp:button id="DeleteRowButton"
        text="Delete Record"
        onclick="DeleteRowButton_Click" 
        runat="server"/>

      <hr/>

      <asp:gridview id="CustomersGridView" 
        allowpaging="true"
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="true"
        autogenerateselectbutton="true"
        datakeynames="CustomerID"
        selectedindex="0"   
        runat="server">
        
        <selectedrowstyle BackColor="lightblue"/>
        
      </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>

<%@ 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 DeleteRowButton_Click(sender As Object, e As EventArgs)
 
    ' Programmatically delete the selected record.
    CustomersGridView.DeleteRow(CustomersGridView.SelectedIndex)
    
  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView DeleteRow Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridView DeleteRow Example</h3>

      <asp:button id="DeleteRowButton"
        text="Delete Record"
        onclick="DeleteRowButton_Click" 
        runat="server"/>

      <hr/>

      <asp:gridview id="CustomersGridView" 
        allowpaging="true"
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="true"
        autogenerateselectbutton="true"
        datakeynames="CustomerID"
        selectedindex="0"   
        runat="server">
        
        <selectedrowstyle BackColor="lightblue"/>
        
      </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>

Comentarios

Use el DeleteRow método para eliminar mediante programación el registro en el índice especificado del origen de datos. Este método se usa normalmente cuando es necesario eliminar un registro desde fuera del GridView control, como desde un control diferente de la página. Al llamar a este método también se generan los RowDeleted eventos y RowDeleting .

Se aplica a

Consulte también