Export (0) Print
Expand All
Expand Minimize

GridView.DeleteRow Method

Updated: July 2009

Deletes the record at the specified index from the data source.

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

public virtual void DeleteRow(
	int rowIndex
)

Parameters

rowIndex
Type: System.Int32

The index of the row to delete.

ExceptionCondition
HttpException

The GridView control is not bound to a data source control.

NotSupportedException

The data source control that the GridView control is bound to does not support delete operations, or there is no delete command defined for the data source.

Use the DeleteRow method to programmatically delete the record at the specified index from the data source. This method is commonly used when you need to delete a record from outside of the GridView control, such as from a different control on the page. Calling this method also raises the RowDeleted and RowDeleting events.

The following example demonstrates how to use the DeleteRow method to programmatically delete a record in a 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>

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

Date

History

Reason

July 2009

Documented exceptions.

Customer feedback.

Community Additions

ADD
Show:
© 2015 Microsoft