GridView.DeleteRow(Int32) Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Tar bort posten vid det angivna indexet från datakällan.
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)
Parametrar
- rowIndex
- Int32
Indexet för raden som ska tas bort.
Undantag
Kontrollen GridView är inte bunden till en datakällakontroll.
Den datakällkontroll som GridView kontrollen är bunden till stöder inte borttagningsåtgärder, eller så har inget borttagningskommando definierats för datakällan.
Exempel
I följande exempel visas hur du använder DeleteRow metoden för att programmatiskt ta bort en post i en GridView kontroll.
<%@ 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>
Kommentarer
DeleteRow Använd metoden för att programmatiskt ta bort posten vid det angivna indexet från datakällan. Den här metoden används ofta när du behöver ta bort en post utanför GridView kontrollen, till exempel från en annan kontroll på sidan. Om du anropar den här metoden genereras RowDeleted även händelserna och RowDeleting .